Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

AJAX Reload PHP/Div Without Refresh

Posted on 2009-04-05
5
Medium Priority
?
4,699 Views
Last Modified: 2012-05-06
I have a page with 2 main divs. I populate these divs using PHP functions with calls to a mySQL database. Here's what I need help with:

My database updates every 4 minutes and I want to "reload" these divs with the new database info every 5 minutes without refreshing the page.

How can I do this? I am a complete AJAX newbie!

Thanks in advance!
0
Comment
Question by:webseth
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Graceful_Penguin
ID: 24074945
Just create a timer set on five minutes and then call the php with a normal ajax call then put the response of the ajax call into the innerhtml of the div.
0
 
LVL 5

Expert Comment

by:striker46
ID: 24074958
This post of mine deals on it. Fully functional code included.

http://www.aleixcortadellas.com/main/?p=232

Regards
0
 
LVL 5

Accepted Solution

by:
striker46 earned 2000 total points
ID: 24074964
The link I posted above deals on DIV refreshing with AJAX in general, I recommend you read the usage of the code. This other post focuses specifically on refreshing a DIV with MySQL retrieved data using AJAX:

http://www.aleixcortadellas.com/main/?p=320
0
 
LVL 9

Expert Comment

by:Graceful_Penguin
ID: 24075157
Here is the javascript code to do what you asked. I assume you can do the php part. Also I did not put in all the error handleing but I am sure you can do that.
function TimerTest()
{
	//for other browsers
	req = new XMLHTTPRequest();
	//for ie
	//req = new ActiveXObject("Microsoft.XMLHTTP");
	if (req != null)
	{
		req.open("GET", YourURL , false);
		req.send();
		if (req.responsetext != '')
		{
			divtag = document.getElementById('YOURDIVTAG');	
			divtag.innerHTML = req.responsetext;
		}
		else
			alert('error');
	}
	var t=setTimeout("TimerTest();",1800000);
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:webseth
ID: 31566885
Excellent. Worked perfectly. Ended up going with the "Multiple Div" Tut. Thanks!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

783 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