Hoping this is my last question for this form I made. My php works fine and is getting the correct data. I've created variables for the indexes and now want to pass them to my js/ajax so the values fill the html textboxes. Not sure code is correct or not as there are not a lot of examples of what I am doing out there.
Am I calling the array indexes correctly in the ajax portion of this page? Not sure if I should be using 'innerHTML' or the value attribut. If i'm way off please explain how this can be done. If you need to see my php I can edit. Thanks!
<script type="text/javascript">
function queryStudent() {
var ajaxRequest;
try {
ajaxRequest = new XMLHttpRequest;
}catch(e) {
//IE Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Browser not compatible");
return false;
}
}
}
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
document.getElementById("studentID").value = output1;
document.getElementById("stuName").value = output2;
document.getElementById("email").value = output3;
document.getElementById("GPA").value = output4;
}
};
var student_ID = document.getElementById('studentID').innerHTML = ajaxRequest.responseText;
var queryString = "?studentID=" + student_ID;
ajaxRequest.open("GET", "search_single.php"+ queryString, true);
ajaxRequest.send(); //Return value from function
}
</script>
<h2>Search for Students by studentID</h2>
<br>
<form name="myForm" action="search_single.php" method="post"> <!--no action or post as it is posted to same page-->
<label>Student ID:</label>
<input type="text" id="studentID">
<br>
<br>
<input type="button" onClick="queryStudent()" value="Search">
<h2>Student information will be displayed in the textboxes below:</h2>
<br>
<table id="outuput">
<tr>
<td>Student ID:</td>
<td><input type="text" id="studentID" value="" readonly></td>
</tr>
<tr>
<td>Student Name:</td>
<td><input type="text" id="stuName" value="" readonly></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" id="email" value="" readonly></td>
</tr>
<tr>
<td>GPA:</td>
<td><input type="text" id="gpa" value="" readonly></td>
</tr>
<br>
</table>
</form>
PHP Code:
if(!isset($studentID)) {
$studentID = filter_input(INPUT_GET, 'studentID', FILTER_VALIDATE_INT);
if($studentID == NULL || $studentID == FALSE) {
$studentID = 1;
}
}
//prepare query
$query = 'SELECT * FROM student
WHERE studentID = :studentID';
$statement = $db->prepare($query);
$statement->bindValue(':studentID', $studentID);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
//var_dump($result); //check indexes
$output = "";
$output1 = $output.$result['studentID'];
$output2 = $output.$result['name'];
$output3 = $output.$result['email'];
$output4 = $output.$result['GPA'];
?>