2013-04-21 5 views
0

Я пытаюсь вытащить студентов через форму на основе ajax из базы данных mysql на основе ввода gpa из html-формы.Попытка вытащить данные из 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; 
?> 

Форма работает должным образом, однако, когда я нажимаю мою кнопку отправки, ничего не происходит. Я не вытаскиваю никаких ошибок из любого места, поэтому я не могу понять, что там происходит. Есть ли способ увидеть явные ошибки или что-то еще? Я не уверен, почему он не извлекает данные из базы данных.

ответ

0

Вам нужно изменить ajaxDisplay.value = ajaxRequest.responseText; к ajaxDisplay.innerHTML = ajaxRequest.responseText;

+0

спасибо за это ... – Kenny

Смежные вопросы