1
votes

This is the very first time i am using AJAX. I totally don't understand the flow of data here. please help.and i am doing it in codeigniter This is my code EDITED:

The js function in my view page:

    function selectHandler() {
      var selectedItem = chart.getSelection()[0];

      if (selectedItem) {
        var topping = data.getValue(selectedItem.row, 0);

       $.post("http://localhost/offlearn/index.php/ctrl_offapp/trail2",
    {
      top: topping,

    },
    function(res,status){
       // alert(res);
        document.getElementById('tablePrint').innerHTML = res;
    });
    }
    }

trial2 is my controller function.

Can you help?

**The trial2 function in codeigniter is ** :

    public function trail2(){
$var=$this->input->post('top');
$data['ts'] = $this->offapp_database->get_tasks();
$data['u'] = $this->offapp_database->usermaster();
print_r($data['ts']);
exit;
$myTable= '<table><tr><td >TASK NAME</td><td >ASSIGNED TO</td><td >CREATED BY</td></tr>';

 foreach($ts->result() as $tk)
     {  
     if ($tk->status ==  0 ) {         
     $myTable.= '<tr><td >'.$tk->taskname.'</td><td >';  foreach($u->result() as $usr)
     { if ($usr->id ==  $tk->assignto) {echo $usr->fname;}}
     $myTable.= '</td><td >';
    foreach($u->result() as $usr)
     { if ($usr->id ==  $tk->createdby ) {echo $usr->fname;}}
     $myTable.=' </td></tr>';
     $myTable.='</table>';
     echo $myTable ;
}
   }    
 }

The functions inthe model:

    public function get_tasks()
    {
     $this->db->select('*');
     $this->db->from('tasks ');
     $rslt = $this->db->get();
     return $rslt;
     }
   public function usermaster()
  {
   $this->db->select('*');
   $this->db->from('usersmaster ');
   $rslt = $this->db->get();
   return $rslt;
  }

here i tried to display the the table assigned to the variable $myTable in the div 'tableprint' of the view page. and this is the output i get:

CI_DB_mysqli_result Object ( [conn_id] => mysqli Object ( [affected_rows] => 108 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 19 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.5.5-10.1.16-MariaDB [server_version] => 50505 [stat] => Uptime: 639 Threads: 1 Questions: 11 Slow queries: 0 Opens: 20 Flush tables: 1 Open tables: 13 Queries per second avg: 0.017 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 3 [warning_count] => 0 ) [result_id] => mysqli_result Object ( [current_field] => 0 [field_count] => 14 [lengths] => [num_rows] => 77 [type] => 0 ) [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => [row_data] => )

Please help me to know where i went wrong

3

3 Answers

0
votes

You can't. You seem to have bad understanding of the way your webpage is generated and handled, and according to your answer I believe it's AJAX which mingled things in your head.

PHP code is interpreted by the server. JS code is interpreted by the browser.

However, AJAX made it possible to call PHP code from JS code, I believe that's what confuses you.

AJAX consists of sending HTTP requests from JavaScript code and receiving the HTTP response, which allow a developper to, among many other things, send data to a PHP script and receive other data.

But keep in mind that when you do that, the only PHP code that's used by your JS is the code that answers your AJAX request. You can't have PHP code in your page that is magically interpreted by your browser so that it can use data sent by AJAX.

What makes me even more assume that you tried to apply AJAX without understanding it is that the data you receive after your AJAX call has been presumably made and transmitted by PHP, why not apply whatever PHP code you want to apply on that res data at that point, before your PHP code sends it to your page ?

0
votes

Generate the HTML in http://localhost/offlearn/index.php/ctrl_offapp/trail2 and return it.

Then, in the AJAX callback, set the html with the following: ...

}, function(res, status) {
    $('#tablePrint').html(res);
});

...

0
votes

You should be more clear to ask a question as I do understand your function is a javascript function which you can call like this in your code.

function selectHandler() {
  var selectedItem = chart.getSelection()[0];
  if (selectedItem) {
    var topping = data.getValue(selectedItem.row, 0);
    $.post("http://localhost/offlearn/index.php/ctrl_offapp/trail2", {
      top: topping,  
    }, function(res, status) {
      //alert(res);
      test(res);
    });
  }
}