I have a PHP program that uses AJAX but for some reason the update page (response text) is not responding. I cannot seem to find what wrong and any help will be greatly appreciated.
The program simply goes into the database and gets a date from a field and uses AJAX to send it to the update page responsible for the response text. The update page then simply gets the date it was sent,gets the current and returns it to the original page that then displays both dates in an alert box.
ajax_test.php:
<html>
<head>
<script>
var tmr;// timer
var xmlhttp;
function UpdateTable()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var tokens = xmlhttp.responseText.split("|");
alert(tokens[0]+", "+tokens[1]);
}
}
xmlhttp.open("GET","update_ajax_test.php?date1="+newDate,true);
xmlhttp.send();
}
</script>
</head>
<body onload="tmr=setInterval(UpdateTable,5000)">
<?php
mysql_connect("xxxxxxxxxxx", "xxxxxxxxx", "xxxxxxxx") or die("not logged in");
//////now selecting our database
mysql_select_db("xxxxxxxxx") or die(mysql_error());
$res = mysql_query("SELECT num FROM table1");
$numEnter=$_GET['name'];
echo'<form method="get" action="ajax_test.php">';
echo' <input type="text" name="name"><br>';
echo' <input type="submit" name="submit" value="Submit Form"><br>';
echo'</form>';
date_default_timezone_set('America/New_York');
$date = date('Y-m-d H:i:s');
echo $date;
mysql_query("INSERT INTO table1(num,last_updated) VALUES ('$numEnter','$date')");
echo"<table border=1 id='t1'>";
while ($row = mysql_fetch_array($res))
{
echo"<tr><td>";
echo $row['num'];
echo"</td></tr>";
}
echo"</table>";
?>
<script>
var newDate ;
newDate=<? echo $date; ?>;
</script>
</body>
</html>
update_ajax_test.php
<?php
//////connecting to our sql server
mysql_connect("xxxxxxxxxxxxx", "xxxxxxxxxxx", "xxxxxxxxxx") or die("not logged in");
//////now selecting our database
mysql_select_db("xxxxxxxxx") or die(mysql_error());
$former_page_date=$_GET['date1'];
$query3=mysql_query("SELECT last_updated FROM table1 ORDER BY id DESC LIMIT 1");
while($row=mysql_fetch_array($query3)){
$update_page_date=$row['last_updated'];
}////END WHILE
echo $update_page_date;
echo "|";
echo $former_page_date;
?>