Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

PHP On-The-Fly!

By Dennis Pallett
2005-05-26


Example 1

First, copy the code below and save it in a file called 'script.js':


var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

function loadFragmentInToElement(fragment_url, element_id) {
var element = document.getElementById(element_id);
element.innerHTML = '<em>Loading ...</em>';
xmlhttp.open("GET", fragment_url);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
element.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}


Then copy the code below, and paste it in a file called 'server1.php':


<?php
echo date("l dS of F Y h:i:s A");
?>


And finally, copy the code below, and paste it in a file called 'client1.php'. Please note though that you need to edit the line that says 'http://www.yourdomain.com/server1.php' to the correct location of server1.php on your server.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
<head>
<title>Example 1</title>
<script src="script.js" type="text/javascript"></script>

<script type="text/javascript">
function updatedate() {
loadFragmentInToElement('http://www.yourdomain.com/server1.php', 'currentdate');
}

</script>
</head>

<body>
The current date is<span id="currentdate">
<?php echo date("l dS of F Y h:i:s A"); ?>
</span>.<br /><br />

<input type="button" value="Update date" OnClick="updatedate();" />
</body>

</html>


Now go to http://www.yourdomain.com/client1.php and click on the button that says 'Update date'. The date will update, without the page having to be reloaded. This is done with the XML HTTP Request object. This example can also be viewed online at http://www.phpit.net/demo/php%20on%20the%20fly/client1.php.



Tutorial Pages:
» Introduction
» How does it work?
» Example 1
» Example 2
» Any Disadvantages...?
» Conclusion


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.