Smudger
Smudger

Reputation: 10809

jQuery autocomplete with Codeigniter, not working outside of index function

Tearing my hair out with Codeigniter and jquery.

I have a very basic code set currently to troubleshoot this.

Currently my index function calls my view. the view processes the jquery autocomplete perfectly with this scenario.

if I change index to indextest or any other name the autocomplete stops working. almost like the jquery output is being passed specifically to the default method of the page, being index.php.

is there a way to specify the method is must be returned to or displayed on?

my controller is:

<?Php

class Sales extends MY_Controller{

  function __construct() {
    //call parent constructor
    parent::__construct();
    $this->load->model('sales_model');
  }

  function index(){
    $this->load->view('sales/new_order_details');
  }

  function get_customers(){
    $this->load->model('Sales_model');
    if (isset($_GET['term'])){
      $q = strtolower($_GET['term']);
      $this->Sales_model->get_customer($q);
    }
  }

  function login() {
    redirect('/pointer/login');
  }

  function logout() {
    redirect('/pointer/logout');
  }
}

My Model

<?php
// (Array of Strings)
class Sales_model extends MY_Model{

  function get_customer($q){
    $this->db->select('CustomerName');
    $this->db->like('CustomerName', $q);
    $query = $this->db->get('Customers');
    if($query->num_rows > 0){
      foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['CustomerName'])); //build an array
      }
      echo json_encode($row_set); //format the array into json data
    }
  }
}

My View

<html>
   <head>
      <title>
         Capture New Order
      </title>

      <link href="<?php echo base_url() ?>application/css/ui-lightness/jquery-ui-1.8.custom.css" media="screen" type="text/stylesheet" rel="stylesheet">
      <script src="<?php echo base_url() ?>application/scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
      <script src="<?php echo base_url() ?>application/scripts/jquery-ui-1.10.1.custom.min.js" type="text/javascript"></script>
      <script src="<?php echo base_url() ?>application/scripts/autocomplete.js" type="text/javascript"></script> 
   </head>
<body>
   <div  data-role="page"  data-theme="a">
      <div class="wrap-header">
         <div data-role="header" data-mini="true"  data-ajax="false">
            <a data-icon="grid" data-mini="true"  data-theme="a" onclick="window.location.href='/pointer'">Menu</a>
            <h3>New Order Details</h3>
         </div>
      </div>   
   <div data-role="content">
   <form>
      <label for="customer">Customer</label>
      <input type="text" id="customer" />
   </form>
   </div>

</body>
</html>

autocomplete.js

$(function(){
  $("#customer").autocomplete({
    source: "sales/get_customers"
  });
});

So as mentioned, if I change my index() method of the controller to indextest() and browse directly to that method the autocomplete stops working.

Am I missing something simple or is there a greater reason I cant work out?

Thanks as always for the help,

UPDATE AS PER FABIO the output from google chrome developer on calling the autocomplete script

normal index()working) enter image description here

indextext() enter image description here

Upvotes: 1

Views: 1382

Answers (2)

Eldar
Eldar

Reputation: 591

The index method within controller Sales gets called by default if a second uri parameter is not present, you have actually answered you self.

Because you now load the view using the url sales/indextest you need to adjust your source url, sales/get_customers is a relative url which works when you load the auto complete without specifying a second parameter indextest, as for the solution you might want to add a full url to the source for example soruce:"<?=base_url();?>sales/indextest"

You also might want to output json as following:

$this->output->set_content_type('application/json')->set_output(json_encode($data));

insted of:

json_encode($row_set);

Upvotes: 1

Fabio Antunes
Fabio Antunes

Reputation: 22882

On this

    $(function(){
  $("#customer").autocomplete({
    source: "sales/get_customers"
  });
});

Do this

$(function(){
  $("#customer").autocomplete({
    source: "<?=site_url('sales/get_customers')?>"
  });
});

Upvotes: 3

Related Questions