hello, i am a beginner to ajax.
i have created a mysql database, which i would like to access from a web page.
i have created 3 files, a html to display the data, a php file to extract the data, and a javascript file to to the clever stuff.
when i access the html page, all the data is displayed correcty, but if i add or delete any records, and press the button i have created to refresh the data(without reloading), the data doesn't change. can anyone help?
here is my code:
My HTML
[HTML]<!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>Untitled Document</title>
<script language="javascript" type="text/javascript" src="ajaxconnect.js">
</script>
</head>
<body>
<div id = "buttons">
<form name='form1'>
<input name='ref' type='button' onclick="callAjax()" value="Refresh">
</form>
</div>
<div id="data">
</div>
</body>
</html>
[/HTML]-------------------------------------------------------------------------------------------------------------
my php file
[PHP]<?php
$server = "localhost";
$user = "xxxxx";
$password = "xxxxxx";
$database = "xxxxx";
$db_handle = mysql_connect($server, $user, $password);
$db_found = mysql_select_db($database, $db_handle);
if($db_found)
{
print("<p>connected and db found</p>");
}
drawtable();
function drawtable()
{
$sql = "select * from people";
$result = mysql_query($sql);
print("<table border=1 cellpadding=2>");
print("<th>title</th><th>surnam</th><th>fname</th><th>age</th><th>nationality</th><th>company</th><th>job</th><th>country</th><th> county</th><th>team</th>");
while ($db_field = mysql_fetch_assoc($result))
{
print("<tr><td>".$db_field['title']."<td>".$db_field['surname']."</td><td>".$db_field['fname']."</td><td>".$db_field['age']."</td><td>".$db_field['nationality']."</td><td>".$db_field['company']."</td><td>".$db_field['job_title']."</td><td>".$db_field['country']."</td><td>".$db_field['county']."</td><td>".$db_field['football_team']."</td></tr>");
}
print ("</table>");
}
mysql_close();
?>
[/PHP]
--------------------------------------------------
my ajax code - function getXMLHTTPRequest()
-
{
-
var req = false;
-
-
try
-
{
-
req = new XMLHttpRequest(); /* e.g Firefox */
-
-
}
-
catch(err1)
-
{
-
try
-
{
-
req = new ActiveXObject("Msxml2.XMLHTTP"); /*some versions IE */
-
-
-
}
-
catch(err2)
-
{
-
try
-
{
-
req = new ActiveXObject("Microsoft.XMLHTTP"); /*some versions IE */
-
-
}
-
catch(err3)
-
{
-
req = false;
-
-
}
-
}
-
}
-
return req;
-
-
}
-
-
var myrequest = getXMLHTTPRequest();
-
-
function callAjax()
-
{
-
-
-
//declare a variable to hold some information to pass to the server
-
//var lastname = document.form1.myname.value;
-
//build the URL of the server script we wish to call
-
var url = "myserverscript.php?";
-
//generate a random number
-
var myRandom=parseInt(Math.random()*99999999);
-
//ask our XMLHTTPRequest object to open a server connection
-
myrequest.open("GET", url, + "&rand=" + myRandom, true);
-
//prepare a function responseAjax() to run when the response has arrived
-
myrequest.onreadystatechange = responseAjax;
-
//and finally send the request
-
myrequest.send(null);
-
}
-
-
function responseAjax()
-
{
-
//we are only interested in a state of 4, i.e, "completed"
-
-
-
if(myrequest.readyState == 4)
-
{
-
-
//if server response is "OK"
-
if(myrequest.status == 200)
-
{
-
alert("about to refresh!");
-
var mytext = myrequest.responseText;
-
document.getElementById('data').innerHTML = mytext;
-
//document.write(myrequest.responseText);
-
-
//program executes statements
-
}
-
else
-
{
-
//issue an error message for any other response
-
alert("an error has occured: " + myrequest.statusText);
-
}
-
}
-
}
-----------------------------------------------------------------------------------------
as i say, i don't get any actual errors as such, but the data just doesn't refresh properly....my theory is that the php output is somehow getting stuck into the cache....but i don't know much.....
hope someone can help