2013-04-21 2 views
0

html 폼의 gpa 입력을 기반으로 mysql 데이터베이스에서 ajax 기반 양식을 통해 학생들을 끌어들이려고합니다. 내 제출 버튼을 클릭하면 그러나, 아무 것도 발생하지 않습니다, 양식이 제대로 작동ajax를 사용하여 mysql에서 데이터를 가져 오려고 시도합니다.

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

:

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

가 여기 내 처리 코드입니다 : 여기

내 양식에 대한 코드입니다. 나는 어디에서든지 어떤 오류가 발생하지 않기 때문에 실제로 어떤 일이 일어나고 있는지를 알 수 없다. 눈에 띄게 오류나 뭔가를 볼 수있는 방법이 있습니까? 왜 데이터베이스에서 데이터를 가져 오지 않는지 잘 모르겠습니다.

답변

0

당신은 당신을 감사 ajaxDisplay.value = ajaxRequest.responseText;

+0

ajaxDisplay.innerHTML = ajaxRequest.responseText;에 변경해야 ... – Kenny

관련 문제