Software informational articles

Php on-the-fly! - software



PHP can be used for a lot of altered things, and is one of the most authoritative scripting languages existing on the web. Not to declare it's exceedingly cheap and extensively used. However, one thing that PHP is lacking, and in fact most scripting languages are, is a way to bring up to date pages in real-time, exclusive of having to stock up a page or accept a form.

The internet wasn't made for this. The web browser closes the correlation with the web head waiter as soon as it has conventional all the data. This means that after this no more data can be exchanged. What if you want to do an fill in though? If you're edifice a PHP claim (e. g. a high-quality contented management system), then it'd be ideal if it worked about like a native Windows/Linux application.

But that requires real-time updates. A little that isn't possible, or so you would think. A good case in point of an appliance that works in (almost) real-time is Google's GMail (http://gmail. google. com). The lot is JavaScript powered, and it's very athletic and dynamic. In fact, this is one of the chief selling-points of GMail. What if you could have this in your own PHP websites as well? Guess what, I'm going to show you in this article.

How does it work?

If you want to effect a PHP script, you need to fill a page, accept a form, or a little similar. Basically, a new link to the head waiter needs to be opened, and this means that the browser goes to a new page, down the preceding page. For a long while now, web developers have been using tricks to get about this, like using a 1x1 iframe, where a new PHP page is loaded, but this is far from ideal.

Now, there is a new way of executing a PHP lettering not including having to fill the page. The basis at the back this new way is a JavaScript module called the XML HTTP Apply for Object. See http://jibbering. com/2002/4/httprequest. html for more in a row about the component. It is supported in all major browsers (Internet Voyager 5. 5+, Safari, Mozilla/Firefox and Opera 7. 6+).

With this be against and some custom JavaScript functions, you can coin some fairly impressive PHP applications. Let's look at a first example, which dynamically updates the date/time.

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 collateral blocked conception 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 bit = document. getElementById(element_id);

element. innerHTML = 'Loading . . . ';

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':

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

Example 1

function updatedate() {

loadFragmentInToElement('http://www. yourdomain. com/server1. php', 'currentdate');


The in progress date is .

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

Example 2

Let's try a more cutting edge example. In the next example, the visitor can enter two numbers, and they are added up by PHP (and not by JavaScript). This shows the true power of PHP and the XML HTTP Apply for Object.

This illustration uses the same script. js as in the first example, so you don't need to coin this again. First, copy the code below and paste it in a file called 'server2. php':

And then, copy the code below, and paste it in a file called 'client2. php'. Desire note despite the fact that that you need to edit the line that says 'http://www. yourdomain. com/server2. php' to the accurate locality of server2. php on your server.

Example 2

function calc() {

num1 = document. getElementById ('num1'). value;

num2 = document. getElementById ('num2'). value;

var bit = document. getElementById('answer');

xmlhttp. open("GET", 'http://www. yourdomain. com/server2. php?num1=' + num1 + '&num2=' + num2);

xmlhttp. onreadystatechange = function() {

if (xmlhttp. readyState == 4 && xmlhttp. status == 200) {

element. value = xmlhttp. responseText;



xmlhttp. send(null);


Use the below form to add up two numbers. The fulfil is calculated by a PHP script, and not with JavaScript. What's the improvement to this? You can effect server-side scripts (PHP) lacking having to refresh the page.

+ =

When you run this example, you can add up two numbers, using PHP and no reloading at all! If you can't get this illustration to work, then have a look on http://www. phpit. net/demo/php%20on%20the%20fly/client3. php to see the case in point online.

Any Disadvantages. . . ?

There are only two real disadvantages to this system. First of all, everybody who has JavaScript curved off, or their browser doesn't assist the XML HTTP Appeal Aim will not be able to run it. This means you will have to make sure that there is a non-JavaScript version, or make sure all your visitors have JavaScript enabled (e. g. an Intranet application, where you can call for JS).

Another difficulty is the fact that it breaks bookmarks. Colonize won't be able to bookmark your pages, if there is any dynamic contented in there. But if you're creating a PHP attention (and not a PHP website), then bookmarks are in all probability not very constructive anyway.


As I've shown you, using two very down-to-earth examples, it is exclusively doable to effect PHP scripts, not including having to refresh the page. I be redolent of you read more about the XML HTTP Appeal Be against (http://jibbering. com/2002/4/httprequest. html) and its capabilities.

The clothes you can do are limitless. For example, you could conceive an exceedingly neat paging system, that doesn't compel reloading at all. Or you could build a GUI for your PHP application, which behaves just like Windows XP. Just think about it!

Be aware even if that JavaScript must be enabled for this to work. Lacking JavaScript this will be entirely useless. So make sure your visitors aid JavaScript, or coin a non-JavaScript adaptation as well.

About The Author

Dennis Pallett is a young tech writer, with much come across in ASP, PHP and other web technologies. He enjoys writing, and has in print a number of articles and tutorials. To find more of his work, look at his websites at http://www. phpit. net http://www. aspit. net and http://www. ezfaqs. com

dennispallett@gmail. com

Developed by:
home | site map © 2018