views:

775

answers:

3

Hi, I'm creating a web page that is built using "modules", in other words the page is put together using other php files.

On one side of my page there is a div container with some links next to it, this div container will be used to hold certain information. The information is held within separate php files that I wont to be able to load into the div container with the links I mentioned earlier.

How would I go about doing this with ajax?

Note: I load the php files with include("");

A: 

for what its worth this is how you would update a div with an id of items using prototype:

new Ajax.Updater('items', '/your_remote_script.php');
seengee
A: 

AJAX - here we're talking about involvement of Javascript. So i'll use jQuery here

index.php:

<?php

// your main file
echo '<div id="module">Please wait while loading...</div>';
echo '<script type="text/javascript" src="jquery-1.3.2.js"></script>';
echo '<script type="text/javsacript">/* <![CDATA[ */';
echo '$(window).ready(function(){
         $("#module").load("module.php");
      });';
echo '/* ]]> */</script>';

?>

module.php:

<?php

echo 'Today is '.gmdate('l');

?>
thephpdeveloper
+4  A: 

AJAX shouldn't replace your PHP includes unless there is a particular reason you want to do this (e.g. page load performance is poor and you want to load some sections asynchronously to the rest of the page).

I would carry on using PHP includes, but if you then want to dynamically update those page sections after the first load, you can use Prototype's Ajax Updater.

Bear in mind that this is not like a server-side include; the parts of the page you update, if they are PHP scripts, will not have access to variables that were set in the rest of the page; you'll need to supply these as GET or POST data with the asynchronous request.

Ben James