CodeIgniter Forums

Full Version: jQuery UI Auto Complete Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Community,

A weird code behavior is faced. It might be coding problem. Here is a scenerio.

I am trying to implement auto complete feature using jQuery UI and returning proper json encoded data. Following is a controller call.

Code:
/*Following code works, if I put exit in the end*/
function get_products(){

       if (isset($_GET['name'])){
           $q = strtolower($_GET['name']);
           $this->Product_model->get_product($q);
           exit;
       }

/*Following code does not work because it does not have exit in end*/
function get_products(){

       if (isset($_GET['name'])){
           $q = strtolower($_GET['name']);
           $this->Product_model->get_product($q);

       }
I want to know the difference, it is probably it looks for the view or is there something special which I am missing ?

Regards,
Sam
What does your Product_model::get_product() method look like?
(07-22-2015, 10:47 AM)CroNiX Wrote: [ -> ]What does your Product_model::get_product() method look like?

It returns json encoded data. 


Code:
if($count > 0){
           foreach ($query->result_array() as $row){
               $new_row['label']=htmlentities(stripslashes($row['common_name']));
               $new_row['value']=htmlentities(stripslashes($row['common_name']));
               $row_set[] = $new_row; //build an array
           }

           echo json_encode($row_set);// exit;//format the array into json data

       }
Generally I like to return data from models and output via the controller that called it.

In your controllers get_products() method, is there any more code below this?
Code:
if (isset($_GET['name'])){
           $q = strtolower($_GET['name']);
           $this->Product_model->get_product($q);

}
(07-22-2015, 10:59 AM)swand Wrote: [ -> ]
(07-22-2015, 10:47 AM)CroNiX Wrote: [ -> ]What does your Product_model::get_product() method look like?

It returns json encoded data. 

Code:
echo json_encode($row_set);// exit;//format the array into json data

This is the cause of the problem. Your model should not be using echo, it should return the data to the controller. The controller should then setup the output (usually by loading a view, but if you just want to output the JSON, you can use the output class)
Code:
$this->output->set_content_type('application/json')->set_output($dataFromTheModel);