无法从数据库中获取结果..! 这是我的Index.html
<html>
<head>
<title>Live Search</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function findName(str)
{
$.POST("names.php",{partialName:str},function(data));
$("#result").innerHtml=data;
}
</script>
</head>
<body>
<center>
Enter The Name: <input type="text" onkeypress="findName(this.value)">
<br>
<div id="result">
</div>
</center>
</body>
这是names.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$name=$_POST['partialName'];
$result=mysql_query("SELECT fname FROM name where fname LIKE '%$name';")or die(mysql_error());
while($list=mysql_fetch_array($result))
{
echo "<div>".$result['fname']."</div>";
}
&GT;
我在检查元素时所犯的错误是: 未捕获的ReferenceError:findName未定义为onkeypress @(index):16
答案 0 :(得分:1)
此行$.POST("names.php",{partialName:str},function(data));
将抛出错误(检查控制台)
SyntaxError:expected expression,got')'
ReferenceError:未定义findName
检查jQuery POST以查看
这是一个快速修复(也为输入添加了名称):
<script type="text/javascript">
function findName(str)
{
$.ajax({
type: "POST",
url: "names.php",
data: {
partialName: str
},
success: function(data){
$("#result").html(data);
}
});
}
</script>
HTML:
<input type="text" name="str" onkeypress="findName(this.value)" />
答案 1 :(得分:0)
所以它应该工作..
function findName(str){
$.post("names.php",{partialName:str},function(data){
$("#result").html(data);
});
}
答案 2 :(得分:0)
编辑你的功能:
<script type="text/javascript">
function findName(str)
{
$.ajax({
type: "POST",
url: "names.php",
data: {
partialName: str
},
success: function(data){
$("#result").innerHtml=data;
}
});
}