Hi friends i develop a page in that i need to refresh a part of a webpage,not a whole one in php using Ajax. Please help me to do this , thanks in advance
There is a good guide to how the XMLHttpRequest object works over at http://www.jibbering.com/2002/4/httprequest.html
You just need to use that, with whatever condition you want to trigger your refresh on, and a PHP script that will output just the data you care about.
PHP cannot do this, only a client-side language like Javascript can. That being said, the jQuery framework would allow you to do this really easily with its AJAX library.
Index.php
<div id="scores">
<!-- score data here -->
</div>
Could be refreshed with the following code:
$("#scores").load("index.php #scores");
That would load the contents of #score from index again without refreshing the entire page.
You could even automate it to refresh every 30 seconds using setInterval();
setInterval(function(){
$("#scores").load("index.php #scores");
}, 30000);
You can read more about $.load() at http://docs.jquery.com/Ajax/load#urldatacallback
fastest way is to use jquery load function
let's say the contents you want to change are inside a div
then you can simply:
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$().ready(function() {
$("#dynamic").load("http://url/to/the/dynamic/data");
});
</script>
Here's a basic example using PrototypeJS.
new Ajax.Updater('containerId', '/url/to/get/content', {
parameters: { somename: 'somevalue' }
});
- The first argument is the ID of the container to put the result of the Ajax call in.
- The second argument is the URL to send the request to
- The third argument, in its most basic form, is the list of parameters to send to the URL.
For more details about the Prototype Ajax request, check out the Ajax.Request documentation.
Taking a page from Jonathan's nice jQuery answer, here's how you'd execute the Ajax request on a timer using Prototype's PeriodicalExecuter.
new PeriodicalExecuter(function(pe) {
new Ajax.Updater('containerId', '/url/to/get/content', {
parameters: { somename: 'somevalue' }
});
}, 30);