1

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.

4
  • You shouldn't be echoing the response in the model class. Return the $row_set array from the model and echo it in the controller. Start there and let us know what happens. Commented Feb 23, 2016 at 16:25
  • @nullReference I tried changing that but still same result. It seems as if the get('term') is returning a null value. Here is what I am getting from my error logs: PHP Fatal error: Call to a member function get_customers() on null in /Applications/XAMPP/xamppfiles/htdocs/prop/application/controllers/Customer.php Commented Feb 23, 2016 at 16:32
  • Your model should extends CI_Model & the first letter of your model filename & class definition should be capitalized Commented Feb 23, 2016 at 17:53
  • Try with echo $this->customer_model->get_customers($q); in controller. Commented Feb 23, 2016 at 17:56

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

I am using autoload for all of my models so the model is definitely loading.
The error you are recieving: PHP Fatal error: Call to a member function get_customers() on null in /Applications/XAMPP/xamppfiles/htdocs/prop/application/controllers/Customer.php...states otherwise
I still get the same PHP error even after I load the model in the construct function. I have it autoload in the autoload file and I am loading it in the construct like you did in this answer. Still same error. Any ideas?
Are you loading the 'database' library in the $autoload['libraries'] array? stackoverflow.com/questions/28230695/…
Yeah. So, I tried loading the model text in the controller and it works perfectly. You're right it's not loading the customer_model class and I have no idea why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.