【问题标题】:Jquery token-input not working when populating data from database从数据库填充数据时,Jquery 令牌输入不起作用
【发布时间】:2016-07-17 14:19:26
【问题描述】:

我正在尝试使用 jquery 令牌输入插件 http://loopj.com/jquery-tokeninput/ 填充数据库中的数据。但没有任何显示。我想知道我哪里出错了。

我接受任何替代方法

我搜索的每个示例似乎都不起作用。

我的索引页面

<script type="text/javascript" 
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js">
</script>
<script type="text/javascript" src="src/jquery.tokeninput.js"></script>

<link rel="stylesheet" href="styles/token-input.css" type="text/css" />
<link rel="stylesheet" href="styles/token-input-facebook.css" type="text/css" />
<div>
<input type="text" id="tagcool"   name="blah" />
<input type="button" value="Submit" />       
<script type="text/javascript">
$(document).ready(function() {                                  
   $("#tagcool").tokenInput("phpsearch.php", {
   theme: "facebook",
   preventDuplicates: true,              
});

});
</script>

还有我的 PHP 页面

    <?php
$link = mysqli_connect("localhost","root","","dbname") or die("Couldn't make connection.");

$look = $_GET['q']; 
$arr = array();
$rs = mysqli_query($link,"SELECT * FROM `country` WHERE name like '%".$look."%'");
# Collect the results
while($obj = mysqli_fetch_object($rs)) {
    $arr[] = $obj;
}
# JSON-encode the response
$json_response = json_encode($arr);
# Optionally: Wrap the response in a callback function for JSONP cross-domain support
if($_GET["callback"]) {
    $json_response = $_GET["callback"] . "(" . $json_response . ")";
}
# Return the response
echo $json_response;

?>

我的数据库示例

【问题讨论】:

  • 首先删除插件选项中的额外逗号(不是答案)并解释更多究竟是什么不起作用,控制台上有什么以及更多调试信息
  • 它不返回任何错误或结果。只是说搜索...
  • 网络选项卡中有什么,ajax 状态显示什么?你能创建一个jsfiddle吗?
  • 只选择需要的字段SELECT id, name FROM country 并且jQuery版本已经过时,至少使用1.7+!

标签: javascript php jquery jquery-tokeninput


【解决方案1】:

这行得通。您需要根据您的设置更改以下代码。

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/token-input-facebook.css" type="text/css" />
</head>
<body>
<form name='jqueryAutocompleteForm' id='jqueryAutocompleteForm'>
    <label for='query'>Search:</label>
    <input type='text' name='q' class='form-control input-lg' id='query' placeholder='Please Start Typing'>
    <input type='submit' value='Submit' class='btn btn-info'>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script type="text/javascript" src="src/jquery.tokeninput.js"></script> 
<script type="text/javascript">
        $(document).ready(function() {
            $("#query").tokenInput("php.php", {
                theme: "facebook"
            });
        });
        </script>
</body>
</html>

注意:确保“jquery.tokeninput.js”和“php to json”的路径,在我的例子中是“php.php”和你的“phpsearch.php”是正确的。

php.php

<?php

    //open connection to mysql db
    $connection = mysqli_connect("localhost","root","","tags") or die("Error " . mysqli_error($connection));


    $s = $_GET["q"];
    //fetch table rows from mysql db
    $sql = "SELECT name from mytable WHERE name LIKE '%".$s."%'";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

    //create an array
    $emparray = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }
    echo json_encode($emparray);

    //close the db connection
    mysqli_close($connection);


?>

替换选择查询,"$sql = "SELECT name from mytable WHERE name LIKE '%".$s."%'";"和你的。

如果您需要知道,我的数据库只有 1 个包含 2 列 1.id、2、名称的表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-16
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多