【问题标题】:Unable to recive values in destination file Typeahead.js无法接收目标文件 Typeahead.js 中的值
【发布时间】:2018-12-06 08:52:44
【问题描述】:

我编写了一些脚本来自动填充搜索输入(从数据库中搜索数据),连同查询一起,我在远程 url 中发送一些动态变量以进行高级搜索。

但是我没有在目标文件中收到任何数据,如果仅在“远程”中发送查询,它工作正常,但与远程 url 中的动态变量一起它不起作用!请检查我的代码并指导我哪里错了

<script src="typeahead.min.js"></script>
   <script>
$(document).ready(function(){
$('input.typeahead').typeahead({
    name: 'typeahead',
    //remote:'subtasksearch.php?key=%QUERY',
    //remote:'subtasksearch.php?key=%QUERY&&mani=123',
    remote: {
    url: 'subtasksearch.php?key=%QUERY&&mani=',
    replace: function () {
        var q = 'subtasksearch.php?key=%QUERY&&mani=';
        if ($('#country').val()) {
            q += encodeURIComponent($('#country').val());
        }
        return q;
      }
    },
    limit : 10
});
 });
</script>

而我的 subtasksearch.php 是

<?php
$key=$_GET['key'];
$mani=$_GET['mani'];
$array = array();
$con=mysqli_connect("localhost","","","");

if (mysqli_connect_errno())
 {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$query=mysqli_query($con, "select * from sub_task where name LIKE '%{$key}%'");
while($row=mysqli_fetch_assoc($query))
{
  $array[] = $row['name'];
  //$array[] = $mani;
}
echo json_encode($array);

我的搜索输入是

 <input type="text" name="typeahead" class="typeahead tt-query" autocomplete="off" spellcheck="false" placeholder="Type your Task" required="">

如果您使用简单的遥控器发送查询,它的工作正常

remote:'subtasksearch.php?key=%QUERY',
//or
remote:'subtasksearch.php?key=%QUERY&&mani=123',

远程 url 中的动态变量不起作用,我在控制台中观察到一些事情,数据在 URL 中传递并返回空数组,我想请检查下面的图像

请指导我哪里错了!我是否以错误的方式发送值?

【问题讨论】:

  • 我了解到远程选项不再具有替换功能。相反,您应该使用 prepare: ,我已经更新了我的问题,请检查

标签: javascript php jquery typeahead.js


【解决方案1】:

我认为问题在于您如何发送查询变量 - 您发送的是文字 %QUER 而不是实际的查询值。

url: 'subtasksearch.php?key=%QUERY&&mani=',

应该是包含变量的东西

url: 'subtasksearch.php?key='+QUERY+'&&mani=',

不确定 typeahead 插件,但也许 this.val() 足以获取值,例如:

url: 'subtasksearch.php?key='+this.val()+'&mani=',

另外,请考虑使用 Prepared Statements 来修补注入风险(即使在 MySQLI 中):How can I prevent SQL injection in PHP?

更新

因此,查看您使用的实际插件,您似乎需要为建议框配置 Bloodhound,而不仅仅是独立的 typeahead。这意味着包括 Bloodhound javascript 源代码或使用捆绑包。

<script src="http://twitter.github.com/typeahead.js/releases/latest/typeahead.bundle.min.js"> </script>
<script> 
var tasks = new Bloodhound({
    remote: {
        url: 'subtasksearch.php?key=%QUERY.json',
        wildcard: '%QUERY'
    }
});

$('input.typeahead').typeahead({
    name: 'typeahead',
    source: tasks,
    limit: 10
  });
 </script>

第二次更新

我不确定 Bloodhound 在屏幕后面做了什么,它是否允许您像这样构建查询:

var country = $('#country').val()
var tasks = new Bloodhound({
    remote: {
        url: 'subtasksearch.php?key=%QUERY.json'+'&country='+country,
        wildcard: '%QUERY'
    }
});

【讨论】:

  • 不,它说“未捕获的 ReferenceError:未定义查询”
  • 您必须明确定义查询变量(基于您实际想要的)。如果您想发送预先输入的值,可以是 $('input.typeahead').val() 或 this.val()。
  • 谢谢,但我不能在这里传递我的动态变量!
  • 嗨 Manikanta,我在查看插件后更新了我的答案。
  • 查看 Manikanta 上方的第二次尝试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
相关资源
最近更新 更多