2014-05-12 143 views
0

我有這樣的HTML代碼,我要搜索的一年學期:PHP Live搜索不顯示結果

<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

+0

如何向我們展示瀏覽器錯誤控制檯的輸出? – bodi0

+3

你很容易被__sql注入___。和'mysql_ *'已被棄用。學習mysqli或pdo。 – itachi

+0

當您使用opera時,我編輯了我的問題 – LeRandomGirl

回答

0

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_*函數。

+0

它不工作:( – LeRandomGirl

+0

@LeRandomGirl你可以再試一次嗎?我鏈接了一個演示它的jsFiddle。 – kelunik