开发者

JQuery ui autocomplete with codeigniter

OK, I am trying to use autocomplete with codeigniter. I did this exact method using regular HTML, JQuery and php an it worked. I tried to modify a bit to make it work with codeigniter but its not working.

The JQuery

$("#update-text").autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>",dataType:"json"});

The function autocomplete in userProfile controller

function autocomplete(){
    // this takes the text field and whatever the user writes it autocompletes it.
    //Every single place and event in the database should be displayed in this view in this format



    $this->load->view("source", $data);

    }

The form in the php file

<form method="post" action="#" name="updatePlanForm">
<div class="ui-widget">
<label for="update-text"></label>
<input type="text" id="update-text" name="updateText" value="What are you gonna do today?" onclick="removeText()"/>
</div>
<input type="button" class="small green button" value="Update Plan" name="updatePlanButton"/> <!-- once clicked JQuery sends a post to a controller send_plan and jquery will return the view -->
</form>

and finally the source php file

<?php

$req = $_GET['term']; //first get the search keyword as get method

$arrResults = array('orange', 'apple', 'bannana');

$array = array_filter($arrResults, 'mycallback');
//filter the array containing search word using call back function

function mycallback($var)
{
    global $req;
    if(preg_match('/^'.$req.'/', $var))
    {       
        return $var;
    }
}

$array1 = array();

//filter null array
foreach($array as $arr => $val)
{
        if(!empty($val))
        {
                $array1[] = $val;
        }

}

//echo out the json encoded array
echo json_encode($a开发者_JS百科rray1);

?>


You shouldn't really have logic like that in your views. Also, the $_GET[] variable won't be populated with any data when you load a view from a controller. In fact $_GET[] won't work at all as query strings are turned off by default in CI. You could turn them on, but you don't need to in this case. A more appropriate solution could be implemented as follows:

First put the autosuggest php code directly into the controller, like so:

function autocomplete () {
  $req = $this->input->post('term');

  $arrResults = array('orange', 'apple', 'bannana');

  $array = array_filter($arrResults, 'mycallback');
  // filter the array containing search word using call back function

  function mycallback ($var) {
    global $req;

    if (preg_match('/^'.$req.'/', $var)) {
      return $var;
    }
  }

  $array1 = array();

  // filter null array
  foreach ($array as $arr => $val) {
    if(!empty($val)) {
      $array1[] = $val;
    }
  }

  //echo out the json encoded array
  echo json_encode($array1);
}

Then change your jQuery call to use POST instead of of GET

$('#update-text').autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>", dataType:'json', type:'POST'});

There are better ways to implement the search, but this should get you on the right track. If you end up connecting this to a database, a simple LIKE query against the 'term' will work fine :)


I know this is super late, but an easy way of fixing jQuery UI autocomplete for CodeIgniter is by modifying the autocomplete portion of the JS file to set the ajax type to POST instead of GET.

Just search for the section labelled "autocomplete" in your jQuery UI .js file, and within this block of code, locate the only ajax call (there should only be one). In the parameters, add

type: "POST"

This should change your autocomplete to use POST rather than GET.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜