autoNameSearch.php
<?php
$conn = mysql_connect("localhost","root","1234");
if(!$conn)
{
die('Could not connect : ' . mysql_error());
}
mysql_select_db("ProductDetails",$conn) or die("Could not select database");
// $searchTag = array();
$sql = mysql_query("select shortSearch from proddetail");
while($row = mysql_fetch_assoc($sql)) {
$search[]= '"'.$row['shortSearch'].'"';
}
productSearch.php
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<!--<link rel="stylesheet" href="/resources/demos/style.css">-->
<script type="text/javascript" > var searchTags = new Array( <?php implode(',', $search)?>); for(var i=0; i<searchTags.length;i++){
searchTags.toString();
document.write("contains: " +searchTags[i]+ "<br>"); } document.write = searchTags;
$(function(){
searchTags
$("#key").autocomplete({
source: searchTags
}); }); </script>
<body>
<form method="post" action="displayProducts.php?go" id="searcform" target="display">
<div class="menubar" id="leftNavMenuBar">
<a href="#" class="hashAdded" style="color: #605f5d">
<i></i>
<i></i>
<i></i>
<span>choose</span></a></div><span id="bms">BMS</span><span id="marketing">MARKETING</span>
<input type="search" name="key" id="key" value="" class="autosearch">
<input type="submit" name="submit" value="FIND">
</form>
</p>
</body>
答案 0 :(得分:0)
将autoNameSearch.php文件更改为以下内容:
<?php
$conn = mysql_connect("localhost","root","1234");
if(!$conn)
{
die('Could not connect : ' . mysql_error());
}
mysql_select_db("ProductDetails",$conn) or die("Could not select database");
// $searchTag = array();
$sql = mysql_query("select shortSearch from proddetail");
$myResults = array();
while($row = mysql_fetch_assoc($sql)) {
//$search[]= '"'.$row['shortSearch'].'"';
array_push($myResults, $row['shortSearch']);
}
echo json_encode($myResults);
这会将您的数据存储在PHP数组中,然后使用PHP json_encode函数将其转换为autoComplete可以读取的json。
答案 1 :(得分:0)
你可以做的很简单(我没看到你在脚本中包含autoNameSearch.php ):
<script type="text/javascript">
$(function(){
$("#key").autocomplete({ /* MAKE SURE YOUR INPUT HAS key ID TAG ON IT */
source: "autoNameSearch.php" /* THE FILE WHERE TO GET THE DATA */
});
});
</script>
您的autoNameSearch.php
文件(我将其更改为mysqli_*
,因为mysql_*
已经deprecated。)
我在您提供的代码中也注意到您未在autoNameSearch.php
文件中打印结果。所以它应该是这样的:
<?php
/* ESTABLISH CONNECTION */
$conn = new mysqli("localhost", "root", "1234", "ProductDetails");
/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if($stmt = $conn->prepare("SELECT shortSearch FROM proddetail")){ /* CHECK PREPARED STATEMENT */
$stmt->execute(); /* EXECUTE QUERY */
$stmt->bind_result($shortsearch); /* BIND THE RESULT TO THIS VARIABLE */
while($stmt->fetch()){ /* FETCH ALL RESULTS */
$search[] = $shortsearch; /* STORE ALL RESULTS TO THIS ARRAY VARIABLE */
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF PREPARED STATEMENT */
echo json_encode($search); /* PRINT THE RESULT IN JSON */
?>