从数据库填充数据时,Jquery令牌输入不起作用

时间:2016-07-17 14:19:26

标签: javascript php jquery jquery-tokeninput

我正在尝试使用jquery token-input插件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;

?>

我的数据库示例

enter image description here

1 个答案:

答案 0 :(得分:2)

这很有效。您需要根据您的设置更改下面的一些编码。

<强> 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);


?>

替换select查询,“$ sql =”SELECTtable from mytable WHERE name LIKE'%“。$ s。”%'“;”和你的一起。

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