Reputation: 1523
I am trying to use the jquery autocomplete widget with codeigniter and keep getting an Internal Server error 500. Here is the code I am using:
View:
<html>
<head></head>
<body>
<input type="text" id="customer_name"/>
<script>
$(document).on('ready', function(){
$("#customer_name").autocomplete({
source: "<?=base_url('customer/customerLookup')?>"
});
});
</script>
</body>
</html>
Controller:
<?php
class customer extends CI_Controller {
public function customerInfo() {
$this->load->view('header');
$this->load->view('customer_view');
$this->load->view('footer');
}
function customerLookup()
{
$q = strtolower( $this->input->get('term'));
$this->customer_model->get_customers($q);
}
}
Model:
class customer_model extends CI_MODEL {
function get_customers($q)
{
$this->db->select('customer_name');
$this->db->like('customer_name', $q);
$query = $this->db->get('customers');
if($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$row_set[] = htmlentities(stripslashes($row['customer_name'])); //build an array
}
return json_encode($row_set); //format the array into json data
}
}
}
I have read through many forums and tried changing the 'enable_query_string' to TRUE also have tried 'uri_protocol'.
Theses are my current CSRF setting:
$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();
I continue to get the Internal server error and cannot figure out why. Help would be much appreciated.
Upvotes: 1
Views: 369
Reputation: 2489
The error you are receiving is because you are not loading your model class into the controller. Without first loading the 'customer_model' the application does not know of it's existence, hints the null value error.
At the beginning of all of my controllers I made a habit of adding a constructor function and including all of the models/helpers/libraries that that controller is going to use within. To do this in your controller add the following code directly after your class declaration:
function __construct() {
parent::__construct();
$this->load->model('customer_model');
}
Also make sure you are loading the 'database' library in:
$autoload['libraries']
of the autoload.php file
Upvotes: 1