所以代码的作用是根据数据库的内容(项目表)向用户提供项目建议。
点击正确的项目建议后,下一个方法(ajax)现在将根据点击的建议检索另一列(项目价格),以在第二个文本框中显示该项目的价格。
当两个方法分开时它已经工作了,所以我猜错误在index.php脚本中。弹出也没有错误,所以我真的需要帮助
的index.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Auto Complete Input box</title>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script>
$(document).ready(function(){
$("#itemTravel").autocomplete("autocomplete.php", {
selectFirst: true
});
function showRoom(){
var itemNamed = $("#itemTravel").val();
$.ajax({
type:"POST",
url:"process.php",
data:{action:"showroom", itemName: itemNamed },
success:function(data){
$("#content").html(data);
}
});
}
showRoom();
});
</script>
</head>
<body>
<label>Tag:</label>
<input name="tag" type="text" id="itemTravel" size="20"/>
<input name="number" type="text" id="itemPrice" size="20"/>
</body>
</html>
process.php
<?php
$link=mysqli_connect("localhost","root","","phpcollab");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$action=$_POST["action"];
$itemName=$_POST["itemName"];
if($action=="showroom"){
echo $itemName;
$query="SELECT * FROM item WHERE itemName='".$itemName."'";
$show=mysqli_query($link,$query) or die ("Error");
$row=mysqli_fetch_array($show);
echo $row['price'];
echo "<script>
document.getElementById('itemPrice').value = '".$row['price']."';
</script>";
}
?>
autocomplete.php
<?php
$q=$_GET['q'];
$my_data=mysql_real_escape_string($q);
$mysqli=mysqli_connect('localhost','root','','phpcollab') or die("Database Error");
$sql="SELECT name FROM tag WHERE name LIKE '%$my_data%' ORDER BY name";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error());
if($result)
{
while($row=mysqli_fetch_array($result))
{
echo $row['name']."\n";
}
}
?>