duan19740319 2011-04-28 10:03
浏览 48

使用codeigniter进行JQuery ui自动完成

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

?>
  • 写回答

2条回答 默认 最新

  • dsfds2353 2011-04-29 01:46
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?