?
Solved

AJAX refresh database query

Posted on 2009-04-29
14
Medium Priority
?
859 Views
Last Modified: 2013-12-12
Hi experts,

I am byuilding a messaging system in php.  I have all MySql tables creauted and have an inbox page which filters the messages table for new messages for the user that is logged in.

Can someone help me with ASJAX to requery this database recordset every 45 - 60 seconds?

Thanks,

Leigh
0
Comment
Question by:Pro-TechIT
  • 6
  • 5
  • 2
13 Comments
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 24267110
Use a javascript timer

http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

and give the interval for 45 seconds (45000 ms)
0
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 24267123
try something like


<html>
<head>
 
<script language="javascript">
setInterval ( "yourAjaxDBCallFunction()", 45000 );
 
function yourAjaxDBCallFunction ( )
{
  // your ajax calls here
}
</script>
 
</head>
 
<body>
</body>
</html>

Open in new window

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24267184
Hi,

For refreshing page:

<head> add <META HTTP-EQUIV=Refresh CONTENT='60; URL=index.php'>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 24267238
Shobinsun,

This will refresh the whole page, so there is no use in using AJAX.
0
 

Author Comment

by:Pro-TechIT
ID: 24267266
Shobinsun - I dont want to meta refresh, i want to refresht he data without the page "flashing"

kiranvj - Thanks, I will look into it.  I had something like that below:
script type="text/javascript">
 
var seconds = 5;
var divid = "MessageDiv";
var url = "ajax.php";
 
////////////////////////////////
//
// Refreshing the DIV
//
////////////////////////////////
 
function refreshdiv(){
 
// The XMLHttpRequest object
 
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}
 
// Timestamp for preventing IE caching the GET request
 
fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}
 
var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;
 
// The code...
 
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}
 
// Start the refreshing process
 
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}
</script>
<script type="text/javascript">
refreshdiv();
</script>

Open in new window

0
 
LVL 16

Accepted Solution

by:
Kiran Paul VJ earned 375 total points
ID: 24267324
this should work perfectly.

<!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 type="text/javascript">
 
var seconds = 5;
var divid = "MessageDiv";
var url = "ajax.php";
 
////////////////////////////////
//
// Refreshing the DIV
//
////////////////////////////////
 
function refreshdiv(){
 
// The XMLHttpRequest object
 
var xmlHttp;
	try
	{
		xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
	}
	catch (e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX.");
				return false;
			}
		}
}
 
// Timestamp for preventing IE caching the GET request
 
fetch_unix_timestamp = function()
{
	return parseInt(new Date().getTime().toString().substring(0, 10))
}
 
var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;
 
// The code...
 
xmlHttp.onreadystatechange=function(){
	if(xmlHttp.readyState==4)
	{
		document.getElementById(divid).innerHTML=xmlHttp.responseText;
		setTimeout('refreshdiv()',seconds*1000);
	}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}
 
// Start the refreshing process
 
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}
</script>
</head>
 
<body>
<div id="MessageDiv"></div>
 
<script type="text/javascript">
refreshdiv();
</script>
</body>
</html>

Open in new window

0
 

Author Comment

by:Pro-TechIT
ID: 24275401
How would I post a variable to var url = "ajax.php";

Would I use var url = "ajax.php?UserID="+$_GET['UserID'];
0
 
LVL 14

Assisted Solution

by:shobinsun
shobinsun earned 375 total points
ID: 24276692
Hi,.

use

 var url = "ajax.php?UserID=".$_GET['UserID'];
0
 

Author Comment

by:Pro-TechIT
ID: 24276805
Thanks shobinsun,

This is throwing an error though.

Not Found

The requested URL /leigh/undefined was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at www.xxxx.com.au Port 80
0
 

Author Comment

by:Pro-TechIT
ID: 24276835
Got it! -

var url = "ajax.php?UserID=<?php echo $_GET['UserID']; ?>";
0
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 24313817
A point split was more appropriate.


K
0
 

Author Comment

by:Pro-TechIT
ID: 24313852
I accepted the wrong one sorry it should  have been kiranvj, how do I modify?
0
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 24314423
I will notify the moderators for the points split.

Thanks for the consideration.

kiranvj
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question