473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ajax beginner: refresh button call doesn't change data

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
2 2219
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
16,027 Expert Mod 8TB
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

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

Similar topics

5
by: darrel | last post by:
I've been playing with prototype.js and scriptaculous to create some nice drag-and-drop interaction on my applications GUI. That's working well. Now I want to take the next step and start passing...
6
by: Nico VanHaaster | last post by:
Hello all, I have run across an issue with IE 6.0+. I have a page that makes an XMLHttpRequest to the webserver to update a report on the page. The first time you hit the refresh report button...
0
by: angiemc | last post by:
Hi, I have a question about AJAX and ASP.Net controls. I'm working on a system that requires pages to be generated dynamically based on XML to provide the layout (eg text boxes, dropdowns, save...
10
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
25
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
1
by: soni2926 | last post by:
hi, we currently have a site done using asp.net 2.0, one of the pages has a datagrid, which displays some information. currently there is a refresh button, but we want to change this so that every...
7
by: raknin | last post by:
I'm using AJAX on my website, but internet explorer does not seem to actually be refreshing the data I retrieve via AJAX when I refresh the page. For example, I have a button that when pressed uses...
4
by: Peter | last post by:
ASP.NET I have an application which use ASP.NET Autocomplete extender which works great. But I have a question how to update all the fields on the screen using Ajax. Users starts typing in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.