Refresh part of a web page in php

I am creating a page in which I need to refresh part of a web page, and not kiss in php using Ajax. Please help me do this, thanks in advance

+7
ajax php
source share
4 answers

PHP cannot do this, only a client language such as JavaScript. In doing so, the jQuery library will allow you to do this very easily using AJAX functionality .

Index.php

<div id="scores"><!-- score data here --></div> 

It can be updated using the following JavaScript:

 $("#scores").load("index.php #scores"); 

This will load the contents of #score from the index again without refreshing the entire page.

You can even automate it to update every 30 seconds with setInterval() ;

 var $scores = $("#scores"); setInterval(function () { $scores.load("index.php #scores"); }, 30000); 

You can learn more about $.load() at http://api.jquery.com/load/#loading-page-fragments .

+37
source share

Here is a basic example using PrototypeJS .

 new Ajax.Updater('containerId', '/url/to/get/content', { parameters: { somename: 'somevalue' } }); 
  • The first argument is the identifier of the container in which the result of the Ajax call is placed.
  • The second argument is the URL to send the request to
  • The third argument in its most basic form is a list of parameters for sending the URL.

For more information on the Prototype Ajax request, see the Ajax.Request documentation.

Taking a page from Jonathan's good jQuery answer, here's how you should execute an Ajax request on a timer using Prototype PeriodicalExecuter .

 new PeriodicalExecuter(function(pe) { new Ajax.Updater('containerId', '/url/to/get/content', { parameters: { somename: 'somevalue' } }); }, 30); 
+3
source share

There is good guidance on how the XMLHttpRequest object works on http://www.jibbering.com/2002/4/httprequest.html

You just need to use this, with any condition for which you want to activate the update, and a PHP script that displays only the data you need.

+1
source share

The fastest way is to use jquery load function

let's say the content you want to change is 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> 
+1
source share

All Articles