hi guys?
Im new to this so please bear with me. I have not seen a practical example of what I am trying to do so i'd appreciate any help I get.
The code below contains a form with a number of fields. I am trying to query the database by entering a number in the first field (student id), then clicking the query button so the query results (student name, other names and class) can be displayed in a div section I have created below the query button.
I have been going through a number of tutorials on this and I've followed all instructions to the best of my knowledge but I cant get it to work.
Clicking the query button results in the following error:
Parse error: parse error, expecting `']'' in C:\xampp\htdocs\project\ajax-example.php on line 46
Can someone please fix this for me??
After the query works I want to make the results of the query to be inserted into the 3 form fields after the student id field. I'd appreciate some guidance on how to go about this as well.
I have attached the sql file of my database just in case someone requires it.
Below is my code. Thanks.
payment.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Payments</title>
</head>
<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
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var stid = document.getElementById('stid').value;
var stfname = document.getElementById('stfname').value;
var stoname = document.getElementById('stoname').value;
var class = document.getElementById('class').value;
var queryString = "?stid=" + stid + "&stfname=" + stfname + "&stoname=" + stoname + "&class=" + class;
ajaxRequest.open("GET", "ajax.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name="payment">
<label class="cf_label" style="width: 150px;">Student Id</label>
<input class="cf_inputbox required validate-number" maxlength="150" size="30" title="" id="stid" name="student_id" type="text" />
<br /><br />
<label class="cf_label" style="width: 150px;">Student First Name</label>
<input class="cf_inputbox required" maxlength="150" size="30" title="" id="stfname" name="student_name" type="text" />
<br /><br />
<label class="cf_label" style="width: 150px;">Student Other Names</label>
<input class="cf_inputbox required" maxlength="150" size="30" title="" id="stoname" name="student_name" type="text" />
<br /><br />
<label class="cf_label" style="width: 150px;">Class</label>
<input class="cf_inputbox required" maxlength="150" size="30" title="" id="class" name="class" type="text" />
<br /><br />
<label class="cf_label" style="width: 150px;">Payment Method</label>
<select class="cf_inputbox validate-selection" id="payment" size="1" title="" name="payment_type">
<option value="">Choose Option</option>
<option value="Banker's Cheque">Banker's Cheque</option>
<option value="Deposit Slip">Deposit Slip</option>
<option value="Cash">Cash</option>
</select>
<br /><br />
<label class="cf_label" style="width: 150px;">Amount</label>
<input class="cf_inputbox required validate-number" maxlength="150" size="30" title="" id="amount" name="amount" type="text" />
<br /><br />
<label class="cf_label" style="width: 150px;">Enter and other additional details</label>
<textarea name="amount" cols="30" class="cf_inputbox required validate-number" id="add" title=""></textarea>
<br /><br />
<label class="cf_label" style="width: 150px;">Names of Parent(s) or Legal Guardian</label>
<input class="cf_inputbox required" maxlength="150" size="30" title="" id="pgname" name="guardian_parents_name" type="text" />
<br /><br />
<input value="Query" onclick='ajaxFunction()' name="button_6" type="button" />
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>
ajax.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "school_database";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$stid = $_GET['stid'];
$stfname = $_GET['stfname'];
$stoname = $_GET['stoname'];
$class = $_GET['class'];
// Escape User Input to help prevent SQL Injection
$stid = mysql_real_escape_string($stid);
$stfname = mysql_real_escape_string($stfname);
$stoname = mysql_real_escape_string($stoname);
$class = mysql_real_escape_string($class);
//build query
$query = "SELECT `student_f.name`,`student_o.names`,`class` FROM `student_dat` WHERE `student_id` = '$stid'";
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Student Id</th>";
$display_string .= "<th>First Name</th>";
$display_string .= "<th>Other Names</th>";
$display_string .= "<th>Class</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[student_id]</td>";
$display_string .= "<td>$row[student_f.name]</td>";
$display_string .= "<td>$row[student_o.names]</td>";
$display_string .= "<td>$row[class]</td>";
$display_string .= "</tr>";
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>