0

我正在尝试根据来自 html 表单的 gpa 输入从 mysql 数据库中通过基于 ajax 的表单来吸引学生。

这是我的表单的代码:

<html>
<body>
<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
 var ajaxRequest;  // The variable that makes Ajax possible!

 try{
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   // Internet Explorer Browsers
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
 // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.value = ajaxRequest.responseText;
   }
 }
 // Now get the value from user and pass it to
 // server script.
 var gpa = document.getElementById('gpa').value;
 var queryString = "?gpa=" + gpa ;
 ajaxRequest.open("GET", "ajax.php" + 
                              queryString, true);
 ajaxRequest.send(null); 
}
//-->
</script>
<form name='myForm'>
GPA: <input type='text' id='gpa' /> <br />
<br />
<input type='button' onclick='ajaxFunction()' 
                              value='Search for students with higher GPA'/>
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>

这是我的处理代码:

<?PHP

include_once ('database.php');

$gpa = $_GET['gpa'];
    // Escape User Input to help prevent SQL Injection
$gpa = mysql_real_escape_string($gpa);

    //build query
$data = $conn->query("SELECT * FROM StudentGPA WHERE GPA = $gpa");
    //Execute query
$qry_result = mysql_query($data) or die(mysql_error());

    //Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Student ID</th>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Gender</th>";
$display_string .= "<th>Major</th>";
$display_string .= "<th>GPA</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
    $display_string .= "<tr>";
    $display_string .= "<td>$row[studentID]</td>";
    $display_string .= "<td>$row[name]</td>";
    $display_string .= "<td>$row[gender]</td>";
    $display_string .= "<td>$row[major]</td>";
    $display_string .= "<td>$row[GPA]</td>";
    $display_string .= "</tr>";

}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>

表单工作正常,但是当我单击提交按钮时,什么也没有发生。我没有从任何地方拉出任何错误,所以我真的看不到那里发生了什么。有没有办法可以明显地看到错误或什么?我不确定为什么它没有从数据库中提取任何数据。

4

1 回答 1

0

您需要更改ajaxDisplay.value = ajaxRequest.responseText;ajaxDisplay.innerHTML = ajaxRequest.responseText;

于 2013-04-21T02:44:20.877 回答