I generally find the the Jquery javascript library works fantasticly for things like this. First you should goto http://www.jquery.com/ and download the latest version.
Copy this downloaded file to your web host and rename it jquery.js
Once you've done that, the following structured HTML will load and use Jquery
Copy it to a file on your web host...
test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Index page 1</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
/* Jquery uses a slightly strange syntax, but once you learn it */
/* it shortens what you have to type considerably */
/* this function is called when the document has finished loading and is ready to proceed */
$(document).ready(function()
{
/* This function is called when the link with ID="link1" is clicked on */
$('#link1').click(function()
{
/* The div tag with ID="newcontent" is loaded via ajax with the new page */
/* nothing else is reloaded...... only the central div */
$('#newcontent').load('page1.php');
/* swap the link colors */
$('#link1').addClass('select');
$('#link2').removeClass('select');
/* Change the title */
document.title = "PHP Page 1";
/* the following line stops the A link from actually executing and doing a page reload */
event.preventDefault();
});
/* This function is called when the link with ID="link2" is clicked on */
$('#link2').click(function()
{
/* The div tag with ID="newcontent" is loaded via ajax with the new page */
/* nothing else is reloaded...... only the central div */
$('#newcontent').load('page2.php');
/* swap the link colors */
$('#link1').removeClass('select');
$('#link2').addClass('select');
/* Change the title */
document.title = "PHP Page 2";
/* the following line stops the A link from actually executing and doing a page reload */
event.preventDefault();
});
/* when this page first loads, we load page1.php into the center content and display it as the default page */
/* and set some defaults */
$('#newcontent').load('page1.php');
$('#link1').addClass('select');
document.title = "PHP Page 1";
});
</script>
<style>
/* CSS styles to set the colours, heights and widths of the various divs */
#header
{
background-color: #EEEEEE;
width: 100%;
height: 60px;
}
#links
{
background-color: silver;
width: 100%;
height: 25px;
}
#newcontent
{
background-color: white;
width: 100%;
height: 200px;
}
#footer
{
background-color: silver;
width: 100%;
height: 25px;
}
/* normal colour of links */
a.noselect
{
color: blue;
}
/* selected colour of links */
a.select
{
color: red;
}
</style>
</head>
<body>
<div ID="header"><h1>Page header logo and text will go here</h1></div>
<div ID="links">
<a href="#" ID="link1" title="Click here to load page 1" class="noselect">[Page 1]</a> |
<a href="#" ID="link2" title="Click here to load page 2" class="noselect">[Page 2]</a>
</div>
<div ID="newcontent">
<p>New content will be loaded here....</p>
</div>
<div ID="footer">This is where the page footer will go</div>
</body>
</html>
The file looks more complicated than it actually is, so don't be scared. Iv'e commented the important bits, and if you've been dealing with divs & styles & such like then the rest should be plain sailing.
the following 2 PHP files should also be put in the same folder as jquery, and test.html on your web host
page1.php
<h1>This is page 1</h1>
<p>It's a PHP file thats called using AJAX by Jquery, and loaded into a static HTML template containing a central DIV</p>
<?php
print "<p>Just to prove that it is indeed PHP, this line is generated using a PHP print statement</p>";
?>
page2.php
<h1>This is page 2</h1>
<p>It's a PHP file thats called using AJAX by Jquery, and loaded into a static HTML template containing a central DIV</p>
<?php
print "<p>Just to prove that it is indeed PHP, Below you can see coloured text..</p>";
print "<hr/>";
$br = rand(0,255);
$bg = rand(0,255);
$bb = rand(0,255);
$background = sprintf("#%2X%2X%2X",$br,$bg,$bb);
$fr = rand(0,255);
$fg = rand(0,255);
$fb = rand(0,255);
$foreground = sprintf("#%2X%2X%2X",$fr,$fg,$fb);
print "<p style='background-color: " . $background . "; color: " . $foreground . ";'>This is text with a random background and forground colour...</p>";
?>
In reality you can use any files you want, they don't have to be PHP, but since this is a PHP forum then i guess they should be ;-)
Also there are probably slightly better ways of doing some of the stuff (Switching the styles for example) but i deliberately tried to keep things simple.
You can find a wealth of tutorials and other info at the jquery website. I use jquery now for ALL my ajax/dom/js requirements in all my php projects.
Cheers
Shawty