Jump to content

HELP: How to AUTO Refresh USing AjAX


eeuser

Recommended Posts

Hi !

I am very new to AJAX.

Can anyone give me an example or links or show me step by step, How to automatically refresh a result from database without refreshing the page using Ajax.

 

Example : Similiar to Yahoo stock updates http://finance.yahoo.com/q?s=yhoo

 

 

Thanks In Advance

 

Newbee

 

Link to comment
https://forums.phpfreaks.com/topic/52358-help-how-to-auto-refresh-using-ajax/
Share on other sites

Hi,

 

I am really new at ajax myself but maybe I can help. I just got something similar going.

 

On mine, the script checks for a change in the db. When there is, it loads a different page. Is that what you had in mind? I'm assuming you are accessing your own db?

 

I used what I think is basic ajax off a website somewhere that check the page every  every ten seconds (see this line setInterval('sndReq()', 100000);)

 

Then, what I believe it does, is it goes to ajax.php which is just a regular php page that queries the db and if the db has changed then it will reload. That is done by "requiring" different pages depending on the db value

 

I posted above a problem I am having with it though. I experimented with iframes and if the displayed page has any animated fetures it causes the entire page to refresh

 

here is the code I use

 

<html>

<head>

<script type="text/javascript">

function createRequestObject() {

  var ro;

  var browser = navigator.appName;

  if(browser == 'Microsoft Internet Explorer') {

    ro = new ActiveXObject("Microsoft.XMLHTTP");

  } else {

    ro = new XMLHttpRequest();

  }

  return ro;

}

var http = createRequestObject();

 

function sndReq(action) {

  http.open('get', 'ajax.php');

 

  http.onreadystatechange = handleResponse;

  http.send(null);

}

 

function handleResponse() {

  if(http.readyState == 4) {

    var response = http.responseText;

   

document.getElementById('foo').innerHTML = response;

  }

}

setInterval('sndReq()', 100000);

</script>

<title>Internet Video Radio (INVIDRA)</title>

</head>

<body bgcolor="#FFFFF0">

<div id="foo">

retrieving data...';

 

</div>

</body>

</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.