Hi guys, im pretty new to ajax and thought i was pretty okish at using mysql and php. Im working on a webpage that allows you to input a persons name and print out competition results associated with that name. I used the following tutorial to learn ajax and my code is near identical. http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php
Index.html:
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
function ajaxFunction(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var age = document.getElementById('name').value;
var queryString = "?name=" + name;
ajaxRequest.open("GET", "getresults.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Competitor Name: <input type='text' id='name' /> <br />
</select>
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
<div id='ajaxDiv'>Your result will display here</div>
</form>
</body>
</html>
getresults.php:
<?php
$dbhost = '';
$dbuser = '';
$dbpass = '';
$dbname = '';
mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die(mysql_error());
$name = $_GET['name'];
$name = mysql_real_escape_string($name);
$query = "SELECT * FROM tablename WHERE Name = '$name'";
$qry_result = mysql_query($query) or die(mysql_error());
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Club</th>";
$display_string .= "<th>FTot</th>";
$display_string .= "<th>Totals</th>";
$display_string .= "</tr>";
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[Name]</td>";
$display_string .= "<td>$row[Club]</td>";
$display_string .= "<td>$row[Ftot]</td>";
$display_string .= "<td>$row[Totals]</td>";
$display_string .= "</tr>";
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>
as you can see the code prints out the query on the page before showing the results. but the query printed is
Query: SELECT * FROM Comp2 WHERE Name = ''
as if it is cut short.
Can anyone point me in the right direction?
Thanks