iTech College - Internet Technology, Online Education, E-Learning, Technician Training, Tech Tutorials, Distance Education, Virtual Learning AJAX Course: Ajax Dynamically Update Data - www.itechcollege.com AJAX Course: Ajax Dynamically Update Data - www.itechcollege.com
Free Courses
HTML
JavaScript
CSS
ASP
PHP
JSP
C++
ASP.NET
SQL
AJAX
FTP
XML

courses > AJAX > Ajax Dynamically Update Data

AJAX Dynamically Update Data

If you go to Yahoo Finance or Google Finance, you may notice that, the stock quotes on their pags are automatically changing without refreshing the page. We can use AJAX to update data dynamically without reloading the page. Let's see how to do it.

Create a page named "display.html". The functions are almost the same:

<html><head>
<script type="text/javascript">
function requestHttp(url,posTag){//posTag for where to show the message
var httpRequest;

if(window.XMLHttpRequest){// Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if(httpRequest.overrideMimeType){
httpRequest.overrideMimeType('text/xml');
}
}else if(window.ActiveXObject){ // IE
try{
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){}
}
}
getFile(httpRequest,url,posTag);
}

function getFile(httpRequest,url,posTag){
if(!httpRequest){
alert('Error - Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function(){
showContents(httpRequest,posTag);//function display contents
}

httpRequest.open('GET', url, true);
httpRequest.send('');

Insert a line to set timeout every 10 seconds. In other words, every 10 seconds, the page will be automotically send a request to the server, so the page contents will be updated every 10 seconds:

setTimeout("requestHttp('banners.php','message')", 10000);//updated every 10 seconds

The rest of codes are the same:

}

function showContents(httpRequest,posTag){
if(httpRequest.readyState == 4){
if(httpRequest.status == 200){
//alert(httpRequest.responseText);
document.all(posTag).innerHTML=httpRequest.responseText;
}else{
alert('Error');
}
}
}

</script>
</head>

We must insert a handler "onload" into the body tag:

<body onload="requestHttp('banners.php','message')">
<span id="message"></span>
</body></html>

The next page is "banners.php". The basic idea is that we put serveal banners into an array, and use a timer to count time and select a banner from the array. For example, when time is A, pick up banner X, and when time is B, pick up banner Y.

Here is the source code of banners.php:

At beginning, use header "no-cashe".

<?php
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");

Create a timer, and use seconds date('s'):

$timstr=date('s');//Ymdhis
$pos=substr($timstr,0,1);

Create a banner array:

$bannerArr=array(
"GPS Watches" =>"http://www.selectagps.com/GPS-Watches-n_35.html",
"Portable Vehicle GPS" =>"http://www.selectagps.com/Portable-Vehicle-GPS-n_21.html",
"Pocket GPS" =>"http://www.selectagps.com/Pocket-GPS-n_8.html",
"Notebooks/Laptops" =>"http://www.ishopsale.com/Notebooks-n_286.html",
"Audio & Video Players" =>"http://www.ishopsale.com/Audio-and-Video-n_50.html",
"Digital Cameras For Sale" =>"http://www.ishopsale.com/Digital-Cameras-n_263.html"
);

Use a loop to search array and select a banner based on the time:

$count=0;

foreach($bannerArr as $k=>$v){
if($count==$pos){
echo '<table width="480" height="60" border="1"><tr><td align="center"><h1>';
echo '<a href="http://'.$v.'">'.$k.'</a>';
echo '</h1></td></tr></table>';
}
$count++;
}
?>

When you run the first page, you will see, the banners are automatically changed every 10 seconds without reloading the page.



  Back to Index Page
 Next Page:  

AJAX Introduction
AJAX Basic
AJAX Display Contents
AJAX Get and Post
AJAX and Database
AJAX and XML
AJAX Dynamically Update Data


Online Education, Online Training for Your Job, Distance Education, E-learning Programs for Your Career!Online Colleges, Online Universities, E-learning & Distance Education for Your Career! Easy Job Training & Online Courses.


   Email A Friend  
 
Earn Online Degree
for Your Job
I found a job after I earned an online degree!
Online classes for finding a Job. Online College, Online University, Degrees, E-learning school, online certificate Earn your
online degree now!
Online Training
for Good Job
Why I work so hard but earn so little? Why I cannot find a high paying job?
Online training for a Good Jobs. Online Degree, E-learning school, online certificate Go to
online school now!

Copyright ©2024, itechcollege.com. All rights reserved
iTechCollege | About iTech | Contact iTech