Forum Moderators: open

Message Too Old, No Replies

update page using ajax

sorry, i know thw answer is somewhere here but i've been unable to get i

         

pojutime

5:16 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



Hello,

I'm new here (my first post). The problem i have is that I want to update the body of my web page say every 10 seconds without having to reload the page i.e. no flickers. I just want any part of the page that has changed e.g. a table connected to a database to show the current data without the users clicking anything i.e. it should be automatic, using a particular time interval like I mentioned.

PLEASE HELP ME!

Thanks.

pojutime

5:47 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



This is my code sample below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script type="text/javascript">
function change()
{
content = document.getElementById("Refreshableregion");
settimeout('change()', 2000);
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout" onload="change();">
<form id="Form1" method="post" runat="server">
<div class="body">
<div id="Refreshableregion">
<asp:DataList id="DataList1" runat="server" Height="128px">
<ItemTemplate>
<b>
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</b>
<%# DataBinder.Eval(Container.DataItem, "Body") %>
</ItemTemplate>
<AlternatingItemTemplate>
</AlternatingItemTemplate>
</asp:DataList>
</div>
</div>
</form>
</body>

pojutime

5:50 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



But it doesn't work. Instead the browser tells me Error on Line 28, Character 3. Can that be the problem? Did I get the code and syntax right? Thanks.

Fotiman

6:31 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use setTimeout or setInterval to call your method every 10 seconds (or whatever). Then have your AJAX methods do the update of the page.

Here's a simple example that uses the Yahoo UI Library's connection manager to handle the AJAX requests. Note, my page is a PHP page 'scheduledajax.php'. All it does it update every few seconds and displays the date from the server.


<?php
// Error Handling
ini_set('display_errors',1);
error_reporting(E_ALL & ~E_NOTICE);
// BACKEND
if( isset($_GET['ajax']) ) {
echo date('l dS \of F Y h:i:s A');
exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title>Scheduled AJAX Example</title>
<script type="text/javascript" src="/yui/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="/yui/build/connection/connection-min.js"></script>
<script type="text/javascript">
// My namespace (to avoid conflicts with global variables)
FOTI = window.FOTI ¦¦ {};
// My page specific object
FOTI.ScheduledAjaxExample = function() {
return {
/**
* (psuedo) Private method for connecting.
*/
_connect : function(method,uri,callback,postdata){
YAHOO.util.Connect.asyncRequest(method,uri,callback,postdata);
},
/**
* Node used to store the results
*/
resultsNode : null,
/**
* A counter, just for testing
*/
counter : 0,
/**
* Initialization method
*/
init : function(e) {
// Get the results node
FOTI.ScheduledAjaxExample.resultsNode = YAHOO.util.Dom.get('results');
if( FOTI.ScheduledAjaxExample.resultsNode!= null ) {
// Start the schedule
setTimeout( FOTI.ScheduledAjaxExample.connect, 3000 );
}
},
/**
* Connect to server
*/
connect : function() {
// AJAX connection stuff goes here
var uri = 'scheduledajax.php?ajax=true&nocache=' + (new Date()).getTime();
var timeout = 2; // seconds
var callback = {
success : function(o){
FOTI.ScheduledAjaxExample.resultsNode.innerHTML = "<div>" + (FOTI.ScheduledAjaxExample.counter++) + " : " + o.responseText + "</div>";
setTimeout( FOTI.ScheduledAjaxExample.connect, 3000 );
},
failure : function(){
alert('Connection failed');
},
timeout : timeout * 1000
};
FOTI.ScheduledAjaxExample._connect('GET', uri, callback, null );
}
};
}();
// Fire my methods once the page loads
YAHOO.util.Event.on(window,'load',FOTI.ScheduledAjaxExample.init);
</script>
</head>
<body>
<div id="results">
When the update occurs, the results will be listed here.
</div>
</body>
</html>

Fotiman

6:34 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I suspect that the error you're seeing is caused from something else. The example you posted looks as though it would execute, though you're not really doing anything... you're just assigning the Refreshableregion to a global variable 'content', but you're not actually doing anything with it.