CodeIgniter Forums
jQuery UI Auto Complete Help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: jQuery UI Auto Complete Help (/showthread.php?tid=62502)



jQuery UI Auto Complete Help - swand - 07-22-2015

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


RE: jQuery UI Auto Complete Help - CroNiX - 07-22-2015

What does your Product_model::get_product() method look like?


RE: jQuery UI Auto Complete Help - swand - 07-22-2015

(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

       }



RE: jQuery UI Auto Complete Help - CroNiX - 07-22-2015

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);

}



RE: jQuery UI Auto Complete Help - mwhitney - 07-22-2015

(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);