By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

ajax beginner: refresh button call doesn't change data

P: 14
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

Expand|Select|Wrap|Line Numbers
  1. function getXMLHTTPRequest()
  2. {
  3.     var req = false;
  4.  
  5.     try
  6.      {
  7.          req = new XMLHttpRequest(); /* e.g Firefox */
  8.  
  9.      }
  10.     catch(err1)
  11.      {
  12.        try
  13.           {
  14.                req = new ActiveXObject("Msxml2.XMLHTTP"); /*some versions IE */
  15.  
  16.  
  17.           }
  18.     catch(err2)
  19.       {
  20.         try
  21.             {
  22.                 req = new ActiveXObject("Microsoft.XMLHTTP"); /*some versions IE */
  23.  
  24.             }
  25.             catch(err3)
  26.              {
  27.                  req = false;
  28.  
  29.              }
  30.       }
  31.      }
  32.       return req;
  33.  
  34. }
  35.  
  36. var myrequest = getXMLHTTPRequest();
  37.  
  38. function callAjax()
  39. {
  40.  
  41.  
  42.     //declare a variable to hold some information to pass to the server
  43.     //var lastname = document.form1.myname.value;
  44.     //build the URL of the server script we wish to call
  45.     var url = "myserverscript.php?";
  46.     //generate a random number
  47.     var myRandom=parseInt(Math.random()*99999999);
  48.     //ask our XMLHTTPRequest object to open a server connection
  49.     myrequest.open("GET", url, + "&rand=" + myRandom, true);
  50.     //prepare a function responseAjax() to run when the response has arrived
  51.     myrequest.onreadystatechange = responseAjax;
  52.     //and finally send the request
  53.     myrequest.send(null);
  54. }
  55.  
  56. function responseAjax()
  57. {
  58.     //we are only interested in a state of 4, i.e, "completed"
  59.  
  60.  
  61.     if(myrequest.readyState == 4)
  62.     {
  63.  
  64.         //if server response is "OK"
  65.          if(myrequest.status == 200)
  66.          {
  67.              alert("about to refresh!");
  68.              var mytext = myrequest.responseText;
  69.              document.getElementById('data').innerHTML = mytext;
  70.              //document.write(myrequest.responseText);
  71.  
  72.              //program executes statements
  73.          }
  74.     else
  75.         {
  76.             //issue an error message for any other response
  77.             alert("an error has occured: " + myrequest.statusText);
  78.         }
  79.     }
  80. }
-----------------------------------------------------------------------------------------

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
Jul 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: 9
Hi frnd,
I think you change your ajax code working your program.

getXMLHTTPRequest() method change the position, i mean i add end of the

method. Ok Any problem reply

Best Wishes BYyyyyyyyyyyyyye
Jul 11 '08 #2

acoder
Expert Mod 15k+
P: 16,027
This line
Expand|Select|Wrap|Line Numbers
  1. myrequest.open("GET", url, + "&rand=" + myRandom, true);
should be
Expand|Select|Wrap|Line Numbers
  1. myrequest.open("GET", url + "&rand=" + myRandom, true);
Please use code tags when posting code.
Jul 11 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.