Just gathering general idea here...

let's say I have a table "Names" with a field "name" --> bob, and tom (2 records)

I want the ajax/php page to pull out all the names. which would be bob and tom. And then if my database got updated at the backend (ex: a new name jack is added). my page showing bob and tom will automatically be updated with jack showing.

Could anyone give me a jump start? thanks.

7 Years
Discussion Span
Last Post by sergb

What you could do is periodically poll the database for list of existing names and use javascript to manipulate HTML in a webpage to display those names.
I suggest using some javascript library that simplifies making AJAX requests and DOM manipulation. One such library is the excellent jQuery. That is what the code below uses.

// Filename: names.php

// $names is an array of strings
$names = get_names_from_database();

// print out names one per line
// note that it is NOT generating the whole html page
// just a portion that will be dynamically injected
foreach ($names as $name)
    echo "$name<br>";

And here is the html page.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function reloadNames() {
    var url = "names.php?t=" + (new Date()).getTime(); //kills browser cache
    // This will make a request to names.php (code above) and put the resulting
    // text (which happens to be valid html) into the names div.
jQuery(function() {
    // Schedule the reloadNames function to run every 5 seconds.
    // So, the list of names will be updated every 5 seconds.
    setInterval(reloadNames, 5000);
    The list of names is below:
    <div id="names"></div>

Of course, the names.php page does not have to return valid HTML. It can produce JSON or any other text that you can manipulate in javascript and do whatever you want with it.

I hope this helps you get started.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.