Php 使用codeigniter的JQuery ui自动完成
好的,我正在尝试将自动完成与codeigniter一起使用。我使用常规的HTML、JQuery和php实现了这个精确的方法,而且效果很好。我试图修改一点,使其与codeigniter一起工作,但它不工作 JQueryPhp 使用codeigniter的JQuery ui自动完成,php,jquery-ui,codeigniter,Php,Jquery Ui,Codeigniter,好的,我正在尝试将自动完成与codeigniter一起使用。我使用常规的HTML、JQuery和php实现了这个精确的方法,而且效果很好。我试图修改一点,使其与codeigniter一起工作,但它不工作 JQuery $("#update-text").autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>",dataType:"json"}); php文件中的表单 <form meth
$("#update-text").autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>",dataType:"json"});
php文件中的表单
<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>
<?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);
?>
最后是源php文件
<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>
<?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);
?>
视图中不应该有这样的逻辑。此外,从控制器加载视图时,$\u GET[]变量不会填充任何数据。事实上,$\u GET[]根本不起作用,因为CI中的查询字符串在默认情况下是关闭的。你可以打开它们,但在这种情况下你不需要。可采用以下更合适的解决方案: 首先将autosuggest php代码直接放入控制器,如下所示:
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);
}
然后将jQuery调用更改为使用POST而不是GET
$('#update-text').autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>", dataType:'json', type:'POST'});
$('updatetext').autocomplete({source:,dataType:'json',type:'POST'});
有更好的方法来实现搜索,但这会让你走上正确的轨道。如果您最终将其连接到一个数据库,那么针对“term”的简单查询就可以了:)您的视图中不应该有这样的逻辑。此外,从控制器加载视图时,$\u GET[]变量不会填充任何数据。事实上,$\u GET[]根本不起作用,因为CI中的查询字符串在默认情况下是关闭的。你可以打开它们,但在这种情况下你不需要。可采用以下更合适的解决方案: 首先将autosuggest php代码直接放入控制器,如下所示:
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);
}
然后将jQuery调用更改为使用POST而不是GET
$('#update-text').autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>", dataType:'json', type:'POST'});
$('updatetext').autocomplete({source:,dataType:'json',type:'POST'});
有更好的方法来实现搜索,但这会让你走上正确的轨道。如果您最终将其连接到数据库,那么针对“术语”的简单查询将很好:)我知道这已经太晚了,但是修复CodeIgniter的jQuery UI自动完成的一个简单方法是修改JS文件的自动完成部分,将ajax类型设置为POST而不是GET 只需在jQueryUI.js文件中搜索标有“autocomplete”的部分,并在这段代码中找到唯一的ajax调用(应该只有一个)。在参数中,添加
type: "POST"
这会将您的自动完成更改为使用POST而不是GET。我知道这已经太晚了,但是修复CodeIgniter的jQuery UI自动完成的一个简单方法是修改JS文件的自动完成部分,将ajax类型设置为POST而不是GET 只需在jQueryUI.js文件中搜索标有“autocomplete”的部分,并在这段代码中找到唯一的ajax调用(应该只有一个)。在参数中,添加
type: "POST"
这会将您的自动完成更改为使用POST而不是GET。很抱歉回复太晚。哪部分不起作用?使用firebug跟踪post请求/响应,并让我知道发生了什么。如果你想使用GET,你必须在你的CI配置中启用查询字符串,但是GET和POST之间的差异并不是问题的原因。很抱歉回复太晚。哪部分不起作用?使用firebug跟踪post请求/响应,并让我知道发生了什么。如果要使用GET,必须在CI配置中启用查询字符串,但GET和POST之间的差异并不是问题的原因。