Easy Ajax Example Using jQuery
This is an easy example of doing basic Ajax with jQuery. This example is short and sweet.
Only two files are needed. First, you're going to need index.html, a basic page that will perform the Ajax request. Then, you're going to need ajax.php that handles the dynamic Ajax request on the server side and responds with a string containing a random number. Save these files off to a folder accessible through your favorite web server capable of rendering PHP.
This page contains several elements with individual ids. The key here is they all have the class dynamic which is used to select each dynamic value in the jQuery example. Every second, it will loop through each of the dynamic elements, make a request to the server, and then populate the contents of each of the elements with the string returned from the server without refreshing the page. It's all automagic.
All this does is returns a string that contains a querystring value and a randomly generated number in a complete string.
<? $id = $_GET['id']; echo "The value is ".rand(1,100)." for id ".$id; ?>Save the two files and open the index.html in a web browser using a URL to your web server. You should see something like the following, updating with different random numbers every second without refreshing the page.
The value is 48 for id 1
The value is 16 for id 2
The value is 97 for id 3
The value is 99 for id 4
The value is 74 for id 5