gpt4 book ai didi

javascript - PHP 实时搜索不显示结果

转载 作者:行者123 更新时间:2023-11-30 00:16:36 25 4
gpt4 key购买 nike

我有这个 html 代码,我想在其中搜索年份和学期:

<script>
function aa() {
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","rankGradePrint.php?nm="+document.form1.t1.value,false);
xmlhttp.send(null);
document.getElementById("d1").innerHTML=xmlhttp.responseText;
}

function bb() {
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","rankGradePrint.php?nmm="+document.form2.t2.value,false);
xmlhttp.send(null);
document.getElementById("d1").innerHTML=xmlhttp.responseText;
}
</script>
<form name="form1" method="post">
<table width="405px" align="center">
<tr>
<td width="5%"><img src="imgs/icSearch.png" height="25"/></td>
<td><input type="text" name="t1" OnKeyup="aa();" class="box3" placeholder="Year"> </td>
</tr>
</table>
</form>
<form name="form2" method="post">
<table width="405px" align="center">
<tr>
<td width="5%"><img src="imgs/icSearch.png" height="25"/></td>
<td><input type="text" name="t2" OnKeyup="bb();" class="box3" placeholder="Semester"></td>
</tr>
</table>

这是我的rankGradePrint.php

<?php $nm1=$_GET['nm'];
$nm2=$_GET['nmm'];
if (empty($nm1)){
echo " ";
exit;
}
if (empty($nm2)){
echo " ";
exit;
}
include('konek.php');
$query1="SELECT t2.schoUsername, t2.schoSurname, t2.schoFirstname, t2.schoMiddlename, t1.gradeAve, t1.gradeSem, t1.gradeSender
FROM tblgrade AS t1 JOIN tblscholar AS t2
ON t1.gradeSender=t2.schoUsername
WHERE t1.gYear LIKE ('$nm1%') AND t1.gradeSem LIKE ('$nm2%')
GROUP BY t1.gradeSender ";
$result = mysql_query($query1) or die(mysql_error());
while($row=mysql_fetch_array($result))
{

echo $row['gradeSem'];
echo " - ";
echo $row['schoSurname'] . ", ". $row['schoFirstname'] . " ". $row['schoMiddlename'];

if($row['gradeAve']<='2.5'){
echo '<h1 style="text-decoration:none; color: #f60b3c;"><strong>'. $row['gradeAve'] .'</strong></h1>';
}
else{
echo '<h1 style="text-decoration:none; color: #000510;><strong>'. $row['gradeAve'] .'</strong>';
}
}
?>

<div id="d1"></div>

我的问题是,当我在文本框中输入内容时,没有结果。我不知道出了什么问题。我的查询有问题吗?或者在我的纸条里?

HERE IS THE PRINTSCREEN

最佳答案

您的should异步执行您的请求,但是您不能使用以下内容:

document.getElementById("d1").innerHTML=xmlhttp.responseText;

相反,您必须指定回调:

xmlhttp.onload = function() {
document.getElementById("d1").innerHTML = this.responseText;
};

我建议使用 POST 进行搜索:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST", "/path/to/your/script.php", true);
xmlhttp.onload = function() {
alert(this.responseText);
};

data = new FormData();
data.append('nm', your_value);

xmlhttp.send(data); // could be a form dom node instead

参见MDN reference .

我还创建了一个 jsFiddle来演示它。

此外,如 itachi已经mentioned in the comments ,您应该使用 MySQLi 或 PDO,而不是已弃用的 mysql_* 函数。

关于javascript - PHP 实时搜索不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23609012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com