prakash Posted November 7, 2007 Share Posted November 7, 2007 How can I update specific portion of a page ( like <div></div>) on every 5 seconds through ajax calling another php file. Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 7, 2007 Share Posted November 7, 2007 The easiest way would be for you to choose a tool set to work with that does your AJAX request handling for you. Then, all you have to do is write a callback function to handle the response. I personally like to use the Ext JS Library or the base [uil=http://developer.yahoo.com/yui/]Yahoo! User Interface Library (YUI)[/url]. Here is an example of how you might run a 5 second update for a div with an ID of "my-ajax-div": HTML: <div id="my-ajax-div"></div> Javascript (using the ExtJS library for the actual request): var con = new Ext.data.Connection(); function updateContent() { con.request({ url: 'my_ajax_page.php', params: 'id=2&cat=15', // This is where you pass your arguments to your AJAX page success: function(r, o) // Callback function for successful request { Ext.get('my-ajax-div').update(r.responseText); // Update content of the div using Ext } }); } window.onload = function() // Start your timer when the window has loaded { setTimeOut("updateContent();", 5000); // Using milliseconds, define your five second interval } Obviously, there are tons of variations you can do, but this at least gives you the principles. Hope this helps some! Quote Link to comment Share on other sites More sharing options...
prakash Posted November 7, 2007 Author Share Posted November 7, 2007 hi isn't there any easy ajax solution. as I went through the site extjs.com and downloaded the library. But I am quite confused what to include and how to start with your given code. Thanks, Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 7, 2007 Share Posted November 7, 2007 hi isn't there any easy ajax solution. as I went through the site extjs.com and downloaded the library. But I am quite confused what to include and how to start with your given code. Thanks, Well, rather than going into a full AJAX tutorial here, I'll recommend you google the solution for an actual request, but here's the principles to do it manually: // Run your AJAX request, and assume myText variable contains your response var mydiv = document.getElementById('my-ajax-div'); function updateContent() { // Run your AJAX request here to get your response mydiv.innerHTML = myText; } window.onload = function() { setTimeOut("updateContent();", 5000); // Using milliseconds, define your five second interval } Quote Link to comment Share on other sites More sharing options...
prakash Posted November 7, 2007 Author Share Posted November 7, 2007 actually I am totally new to Ajax. I used the ajaxrequest.js and used the following code <html> <head> <script type="text/javascript" language="javascript" src="ajaxrequest.js"></script> <title>Test</title> </head> <body onload="MyAjaxRequest('divDataContainer','process.php');"> <div id="divDataContainer"></div> </body> </html> now how can I implement your updateContent() and setTimeOut() idea with my code. Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 7, 2007 Share Posted November 7, 2007 now how can I implement your updateContent() and setTimeOut() idea with my code. Basically, here is what you want. It's not much different than what you already have: <html> <head> <script type="text/javascript" language="javascript" src="ajaxrequest.js"></script> <script type="text/javascript"> window.onload = function() { setInterval("MyAjaxRequest('divDataContainer','process.php');", 5000); } </script> <title>Test</title> </head> <body> <div id="divDataContainer"></div> </body> </html> You, see, you are literally calling the exact same code, but you want to call it within an interval so that it is called every 5 seconds rather than just once at page load. Good luck! Quote Link to comment Share on other sites More sharing options...
prakash Posted November 7, 2007 Author Share Posted November 7, 2007 it works gr8 thanks a lot Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.