Been working on this for 8 hours and haven't gotten anywhere. Made changes only to run into another block. Started over and back to almost square-1. Seeking help in understanding what i'm doing wrong. I did get this working by returning all students in a table but that is not what I need. What I'm trying to do is: 1. Prompt user to enter a student ID. 2. Run the id against a mysql query 3. Output the info about the student (ID, Name, email and GPA) into 4 textboxes. I know that I need an array and then in the js/ajax split the response between the 4 textboxes. How do i call the array in the function and then output the result for that student into the textboxes? Any help or advice is appreciated.
This is what I have so far: html:
<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) {
//delcare a array
//use ajax response.split to return the results of query to each textbox here
var results = ajaxRequest.responseText.split(); //how do i get query results in here
//output array indexes to html element id's
document.getElementById('studentID').value = response[0]; //fill text box 1
document.getElementById('stuName').value = response[1];//text box 2
document.getElementById('email').value = response[2];
document.getElementById('GPA').value = response[3];
}
};
var stuID = document.getElementByID('stuID').value
var queryString = "?stuID=" + stuID;
ajaxRequest.open("POST", "search_single.php"+ queryString, true);
ajaxRequest.send(stuID);
}
</script>
<form name="student" method="post" action="search_single.php">
<label>StudentID:</label>
<input type="text" id="input" name="stuID" value="">
<br>
<br>
<input type="button" onclick="queryStudent();" value="Search" id="button">
</form>
<h2>Student information will be displayed in the textboxes below:</h2>
<br>
<table id="outuput">
<tr>
<td><label>Student ID:</label></td>
<td><input type="text" id="stuID" value="" readonly></td>
</tr>
<tr>
<td><label>Student Name:</label></td>
<td><input type="text" id="stuName" value="" readonly></td>
</tr>
<tr>
<td><label>Email:</label></td>
<td><input type="text" id="email" value=" " readonly></td>
</tr>
<tr>
<td><label>GPA:</label></td>
<td><input type="text" id="gpa" value=" " readonly></td>
</tr>
<br>
</table></body>
PHP:
$stuID = filter_input(INPUT_POST, 'studentID');
//Code for query
$query = 'SELECT *
FROM student
WHERE studentID = :stuID';
$statement = $db->prepare($query); //
$statement->bindValue(':stuID', $stuID); //bind all values (name, email..ect)?
$statement->execute();
$students = $statement->fetch(); //Fetch individual row's
var stuID = document.getElementByID('stuID').valuetovar stuID = document.getElementByID('input').value