简体   繁体   中英

PHP Live Search doesn't show result

I have this html code where i want to search the year and semester:

<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>

And here is my 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>

My problem is, when i type in the textbox, there is no result. I don't know what is the problem. Is there a problem in my query? Or in my scrip?

这里是印刷屏幕

Your should do your request async, but then you cannot use the following:

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

Instead, you have to specify a callback:

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

I'd recommend using POST for a search:

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

See MDN reference .

I've also created a jsFiddle to demonstrate it.

Additionally, as itachi already mentioned in the comments , you should use MySQLi or PDO instead of the deprecated mysql_* functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM