• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ajax/Jquery with Codeigniter Doesn't Access Controller??

#1
[eluser]Kraig[/eluser]
I am trying to use Ajax/Jquery to perform a check. However, the php file with the database query is not working. Below is my code. I know that the code works because when I change the url to a link outside of CI's index it works just fine. The removed part in the code is the script tag in html.

HTML File:
Code:
<button type="button" name="getdata" id="getdata">Get Data.</button>
<div id="result_table">
</div>

  [removed]

   $('#getdata').click(function(){

  //-------------------------------------------------------------------------------------------
  // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
  //-------------------------------------------------------------------------------------------
  $.ajax({                                      
    url: '&lt;?php echo base_url().'jqueryDB/jquery';?&gt;',                  //the script to call to get data    
    type:'POST',      
    data: "",                        //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
    dataType: 'json',                //data format      
    success: function(output_string){
                    $("#result_table").append(output_string);
    }
  });
  
   });
  [removed]

PHP Controller:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class JqueryDB extends CI_Controller {

function __construct(){
        parent::__construct();
    }

function index()
{
  $is_logged_in = $this->loggedin->loggedin();
  if(!$is_logged_in)
  {
   $data['main_content'] = 'home';
   $this->load->view('includes/template2', $data);
  }
  else
  {
   $data['main_content'] = 'jqueryDB';
   $this->load->view('includes/template2', $data);
  }

}

public function jquery(){
        //--------------------------------------------------------------------------
  // 1) Query database for data
  //--------------------------------------------------------------------------
  $userID = $this->session->userdata('id');

  $query1 = $this->db->query("SELECT * FROM test ");
  if($query1->num_rows() > 0){
            $output_string = "";
            $output_string .=  "<table border='1'>\n";
            foreach ($query1->result() as $row){
                $output_string .= "<tr>\n";
                $output_string .= "<td>".$row['id']."</td>\n";
    $output_string .= "<td>".$row['name']."</td>\n";
                $output_string .= "</tr>\n";              
            }                  
            $output_string .= "</table>\n";
        }
        else{
            $output_string = "There are no results";
        }
          
        echo json_encode($output_string);
    }
}

If I change the jquery function in the PHP file to this: it displays "There are no results" when the button is clicked...which is what it should do, so not sure what the problem is
Code:
public function jquery(){

        $output_string = "There are no results";
          
        echo json_encode($output_string);
  
    }

#2
[eluser]Kraig[/eluser]
I got it to work. Below is the code for the controller function:
Code:
public function jquery(){
  $userID = $this->session->userdata('id');
  $query = $this->db->query('SELECT * FROM upload WHERE userID = '.$userID);
  $num_rows = $query->num_rows();
  $val = $query->row_array();    
  
  $output_string = "";  
  
  foreach ($query->result() as $row){
   $output_string .= $row->id;
   $output_string .= " ".$row->name."<br />";
  }
          
        echo json_encode($output_string);
  
    }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.