Welcome Guest, Not a member yet? Register   Sign In
Passing same variable giving different result
#1

[eluser]gazza7364[/eluser]
(Updated every time I post in this thread it missed the part of the onclick=($date) in $output not sure why)

I'm passing the query results back to the view page and also trying to pass back the date which was sent via jquery and json. If I just echo the
Code:
$date
on its own I get 2012-10-09, which is the correct date. But if I send it back with onclick = gatCSV($date) I get 1993.

Here is the controller:

Code:
function query_date()
{
$date= $this->input->post('day');




$sql = "SELECT user.s_name, user.f_name, user.year, it_club.date, it_club.room, it_club.id
FROM user
INNER JOIN it_club
ON user.user_id=it_club.user_id
WHERE it_club.date = '$date'
ORDER BY user.year";  
  $data = $this->it_model->lunch_id($sql);

  $output = null;
  $output .= '<table width = "90%"><th><div class="results">First Name</div></th><th><div "class="results">Surname</th><th><div "class="results">Year</th><th>Date/Time</th><th>Room</th>';
   foreach($data as $names) {
   $output .= "<tr><td>$names->f_name</td><td> $names->s_name</td><td>  $names->year</td><td>  $names->date</td> <td>$names->room</td>";
  }

  $output .= "</table><div class='name'>CSV</div>";
  
  echo $output;
  //just to test the outcome of $date on its own.  
  echo $date;
  }

Can
Code:
$date
be sent this way with on click?



Update: I can see were the the 1993 is coming from, it is calculating the date. 2012-10-09 = 1993. Can this be stopped?
#2

[eluser]gazza7364[/eluser]
I've been trying to output a date result from a query. the date is in the form of 2012-10-15.
The problem is if I send it out with
Code:
<div on click= 'getcsv($result->date)'>CSV</div>,
then when I click the csv link in the view to test the outcome, I get the result 1987, this is because for some reason the $result->date is calculating 2012 - 10 - 15 = 1987. Is this normal, if I echo $data->date; I get the correct result 202-10-15.
I had to split the on click otherwise it won't show in the post.
Any pointers?
#3

[eluser]CroNiX[/eluser]
Because you can't directly trigger a PHP function or access PHP variables with javascript. PHP is executed on the server. After your page is sent to the browser, PHP no longer exists. Javascript is executed in the browser. They are totally separate and don't directly see each other.

A way around that is to use AJAX, which is javascript. When the user clicks on the link, it sends a new HTTP request (behind the scenes) to a controller/method on the server (CI), which receives the request and processes it with PHP, and sends something back to your ajax request. In the "success" event of your AJAX call, you do something with the received data.

I'd look into a javascript framework such as jQuery to simplify this. There are many AJAX tutorials on the net. This isn't specific to CI, so once you get the concept you should be able to implement it.
#4

[eluser]gazza7364[/eluser]
[quote author="CroNiX" date="1350334691"]Because you can't directly trigger a PHP function or access PHP variables with javascript. PHP is executed on the server. After your page is sent to the browser, PHP no longer exists. Javascript is executed in the browser. They are totally separate and don't directly see each other.

A way around that is to use AJAX, which is javascript. When the user clicks on the link, it sends a new HTTP request (behind the scenes) to a controller/method on the server (CI), which receives the request and processes it with PHP, and sends something back to your ajax request. In the "success" event of your AJAX call, you do something with the received data.

I'd look into a javascript framework such as jQuery to simplify this. There are many AJAX tutorials on the net. This isn't specific to CI, so once you get the concept you should be able to implement it.[/quote]

I'm actually using jquery and ajax to send to the controller wiith datepicker to get information from the database, based on a date, which I then output back to the view. What I wanted to do was send the date back, which I sent with jquery, to the view with on click. This is because the results will show a list on users using a room and I needed the same date, so I could give the user the choice of exporting to a csv file if needed. The only other way I can think, is let them use the datepicker to pick the date for exporting.

This is my AJAX code,
Code:
$(".button_day").click(function() {
  // validates and process form
  // first hide any error messages
    $('.error').hide();
  
   var day = $("input#day").val();
  if (start == "") {
      $("label#day_error").show();
      $("input#day").focus();
      return false;
    }
  
  
  
  var dataString = 'day='+ day;
  //alert (dataString);return false;
  
  $.ajax({
      type: "POST",
      url: "/admin/query_date",
      data: dataString,
      success : function (json) {
$("#query").html(json);
}
     });
    return false;
});
});

Thanks for clarifying about AJAX and jquery
#5

[eluser]CroNiX[/eluser]
Ahh, I think I see the problem.
Code:
var dataString = 'day='+ day;

since there isn't a quote around your day variable making it a string, it's performing the math.
Try
Code:
var dataString = 'day="' + day + '"';
#6

[eluser]gazza7364[/eluser]
[quote author="CroNiX" date="1350337858"]Ahh, I think I see the problem.
Code:
var dataString = 'day='+ day;

since there isn't a quote around your day variable making it a string, it's performing the math.
Try
Code:
var dataString = 'day="' + day + '"';
[/quote]

The Jquery AJAX is working fine, its sending the date to the controller and getting the correct results from the data base, what I'm trying to do is send back the date with the results, so I can use the same date, to export to csv if needed.

(Snippet from VIEW)
Code:
$date = 'onClick="getDay()"'

&lt;?=form_button('button1', 'Date',$date);?&gt;

&lt;/form&gt;&lt;form name='contact' action=''>  
      <fieldset class='day'>  
        <label for='day' id='day_label'>Choose Date  </label>  
        &lt;input type='text' name='day' id='day' size='15' value='' class='text-input' /&gt;  
        <label class='error' for='day' id='day_error'>This field is required.</label>


(Jquery AJAX)


$(".button_day").click(function() {
  // validates and process form
  // first hide any error messages
    $('.error').hide();
  
   var day = $("input#day").val();
  if (start == "") {
      $("label#day_error").show();
      $("input#day").focus();
      return false;
    }
  
  
  
  var dataString = 'day='+ day;
  //alert (dataString);return false;
  
  $.ajax({
      type: "POST",
      url: "/admin/query_date",
      data: dataString,
      success : function (json) {
$("#query").html(json);
}
     });
    return false;
});
});


(CONTROLLER Snippet)


function query_date()
{
$date= $this-&gt;input-&gt;post('day');




$sql = "SELECT user.s_name, user.f_name, user.year, it_club.date, it_club.room, it_club.id
FROM user
INNER JOIN it_club
ON user.user_id=it_club.user_id
WHERE it_club.date = '$date'
ORDER BY user.year";  
  $data = $this->it_model->lunch_id($sql);


$sql = "SELECT user.s_name, user.f_name, user.year, it_club.date, it_club.room, it_club.id
FROM user
INNER JOIN it_club
ON user.user_id=it_club.user_id
WHERE it_club.date = '$date'
GROUP BY it_club.date";  
$csv = $this->it_model->lunch_id($sql);


  $output = null;
  $output .= '<table width = "90%"><th><div class="results">First Name</div></th><th><div "class="results">Surname</th><th><div "class="results">Year</th><th>Date/Time</th><th>Room</th>';
   foreach($data as $names) {
   $output .= "<tr><td>$names->f_name</td><td> $names->s_name</td><td>  $names->year</td><td>  $names->date</td> <td>$names->room</td>";
  }
  $output .= "</table>";

  foreach($csv as $data) {
    
  $day = "<div on click='getexport($data-&gt;date)' class='name'>CSV</div>";
  }
  
  echo $output . $day;

}
The problem I have is sending the $day, which holds the date for exporting a csv file if required. Hope this explains my problem. Like I said I could use a date picker to choose the date if their is no other way, I thought I could send it with on click. I think your first explanation, gives me the reason why it won't work.

Thanks again
#7

[eluser]gazza7364[/eluser]
[quote author="CroNiX" date="1350337858"]Ahh, I think I see the problem.
Code:
var dataString = 'day='+ day;

since there isn't a quote around your day variable making it a string, it's performing the math.
Try
Code:
var dataString = 'day="' + day + '"';
[/quote]

The Jquery AJAX is working fine, its sending the date to the controller and getting the correct results from the data base, what I'm trying to do is send back the date with the results, so I can use the same date, to export to csv if needed.

(Snippet from VIEW)
Code:
$date = 'onClick="getDay()"'

&lt;?=form_button('button1', 'Date',$date);?&gt;

&lt;/form&gt;&lt;form name='contact' action=''>  
      <fieldset class='day'>  
        <label for='day' id='day_label'>Choose Date  </label>  
        &lt;input type='text' name='day' id='day' size='15' value='' class='text-input' /&gt;  
        <label class='error' for='day' id='day_error'>This field is required.</label>


(Jquery AJAX)


$(".button_day").click(function() {
  // validates and process form
  // first hide any error messages
    $('.error').hide();
  
   var day = $("input#day").val();
  if (start == "") {
      $("label#day_error").show();
      $("input#day").focus();
      return false;
    }
  
  
  
  var dataString = 'day='+ day;
  //alert (dataString);return false;
  
  $.ajax({
      type: "POST",
      url: "/admin/query_date",
      data: dataString,
      success : function (json) {
$("#query").html(json);
}
     });
    return false;
});
});


(CONTROLLER Snippet)


function query_date()
{
$date= $this-&gt;input-&gt;post('day');




$sql = "SELECT user.s_name, user.f_name, user.year, it_club.date, it_club.room, it_club.id
FROM user
INNER JOIN it_club
ON user.user_id=it_club.user_id
WHERE it_club.date = '$date'
ORDER BY user.year";  
  $data = $this->it_model->lunch_id($sql);


$sql = "SELECT user.s_name, user.f_name, user.year, it_club.date, it_club.room, it_club.id
FROM user
INNER JOIN it_club
ON user.user_id=it_club.user_id
WHERE it_club.date = '$date'
GROUP BY it_club.date";  
$csv = $this->it_model->lunch_id($sql);


  $output = null;
  $output .= '<table width = "90%"><th><div class="results">First Name</div></th><th><div "class="results">Surname</th><th><div "class="results">Year</th><th>Date/Time</th><th>Room</th>';
   foreach($data as $names) {
   $output .= "<tr><td>$names->f_name</td><td> $names->s_name</td><td>  $names->year</td><td>  $names->date</td> <td>$names->room</td>";
  }
  $output .= "</table>";

  foreach($csv as $data) {
    
  $day = "<div on click='getexport($data-&gt;date)' class='name'>CSV</div>";
  }
  
  echo $output . $day;

}
The problem I have is sending the $day, which holds the date for exporting a csv file if required. Hope this explains my problem. Like I said I could use a date picker to choose the date if their is no other way, I thought I could send it with on click. I think your first explanation, gives me the reason why it won't work.

Thanks again




Theme © iAndrew 2016 - Forum software by © MyBB