PHP ON-THE-FLY!

Introduction PHP crapper be utilised for a aggregation of different
things, and is digit of the most coercive scripting languages
available on the web. Not to name it’s extremely affordable and
widely used. However, digit abstract that PHP is lacking, and in fact
most scripting languages are, is a artefact to update pages in
real-time, without having to charge a tender or accede a form.

The internet wasn’t prefabricated for this. The scheme covering closes the
connection with the scheme computer as presently as it has conventional all
the data. This effectuation that after this no more accumulation crapper be
exchanged. What if you poverty to do an update though? If you’re
building a PHP covering (e.g. a high-quality content
management system), then it’d be saint if it worked nearly like
a autochthonous Windows/Linux application.

But that requires real-time updates. Something that isn’t
possible, or so you would think. A beatific warning of an
application that entireness in (almost) real-time is Google’s GMail. Everything is
JavaScript powered, and it’s rattling coercive and dynamic. In fact,
this is digit of the large selling-points of GMail. What if you
could hit this in your possess PHP websites as well? Guess what,
I’m feat to exhibit you in this article.

How does it work? If you poverty to fulfil a PHP script,
you requirement to charge a page, accede a form, or something similar.
Basically, a newborn unification to the computer needs to be opened,
and this effectuation that the covering goes to a newborn page, losing the
previous page. For a daylong patch now, scheme developers hit been
using tricks to intend around this, aforementioned using a 1×1 iframe, where
a newborn PHP tender is loaded, but this is farther from ideal.

Now, there is a newborn artefact of executing a PHP playscript without having
to charge the page. The foundation behindhand this newborn artefact is a
JavaScript factor titled the XML protocol Request Object. See http://jibber
ing.com/2002/4/httprequest.html for more aggregation about
the component. It is based in every field browsers (Internet
Explorer 5.5+, Safari, Mozilla/Firefox and Opera 7.6+).

With this goal and whatever bespoken JavaScript functions, you can
create whatever kinda awesome PHP applications. Let’s countenance at a
first example, which dynamically updates the date/time.

Example 1 First, double the cipher beneath and spend it in a
file titled ’script.js’:

 var xmlhttp=false; /*@cc_on @*/
/*@if (@_jscript_version >= 5) // JScript gives us
Conditional compilation, we crapper manage with older IE versions. //
and section closed creation of the objects. essay { xmlhttp =
new ActiveXObject("Msxml2.XMLHTTP"); } grownup (e) { try
{ xmlhttp = newborn 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 cipher below, and adhesive it in a start titled ’server1.php’:

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

And finally, double the cipher below, and adhesive it in a file
called ‘client1.php’. Please state though that you requirement to edit
the distinction that says ‘http://www.yourdomain.com/server1.php’ to
the precise positioning 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 underway fellow is<span
id="currentdate"><?php reflexion 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 utter on the button
that says ‘Update date’. The fellow module update, without the page
having to be reloaded. This is finished with the XML protocol Request
object. This warning crapper also be viewed online at http://www.phpit.net/demo/php%20on%20the%20fly/client1.php.

Example 2 Let’s essay a more modern example. In the
following example, the traveller crapper start digit numbers, and they
are additional up by PHP (and not by JavaScript). This shows the true
power of PHP and the XML protocol Request Object.

This warning uses the aforementioned script.js as in the prototypal example, so
you don’t requirement to create this again. First, double the cipher below
and adhesive it in a start titled ’server2.php’:

 <?php

// Get drawing $num1 = intval($_GET['num1']); $num2 =
intval($_GET['num2']);

// Return respond reflexion ($num1 + $num2);

?> 

And then, double the cipher below, and adhesive it in a
file titled ‘client2.php’. Please state though that you requirement to
edit the distinction that says ‘http://www.yourdomain.com/server2.php’
to the precise positioning of server2.php on your server.

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

<script type="text/javascript"> duty calc()
{ num1 = document.getElementById ('num1').value; num2 =
document.getElementById ('num2').value;

var surroundings = 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); }
</script> </head>

<body> Use the beneath modify to add up digit numbers. The
answer is premeditated by a PHP script, and
<em>not</em> with JavaScript. What's the advantage
to this? You crapper fulfil server-side scripts (PHP) without
having to change the page.<br /><br />

<input type="text" id="num1"
size="3" /> + <input type="text"
id="num2" size="3" /> = <input
type="text" id="answer" size="5"
/>

<input type="button" value="Calculate!"
OnClick="calc();" /> </body>

</html> 

When you separate this example, you crapper add up
two numbers, using PHP and no reloading at all! If you can’t get
this warning to work, then hit a countenance on http://www.phpit.net/demo/php%20on%20the%20fly/client3.php
to wager the warning online.

Any Disadvantages…? There are exclusive digit real
disadvantages to this system. First of all, anyone who has
JavaScript overturned off, or their covering doesn’t hold the XML
HTTP Request Object module not be healthy to separate it. This effectuation you
will hit to attain trusty that there is a non-JavaScript version,
or attain trusty every your visitors hit JavaScript enabled (e.g. an
Intranet application, where you crapper order JS).

Another separate is the fact that it breaks bookmarks.
People won’t be healthy to marker your pages, if there is any
dynamic noesis in there. But if you’re creating a PHP
application (and not a PHP website), then bookmarks are probably
not rattling multipurpose anyway.

Conclusion As I’ve shown you, using digit rattling simple
examples, it is every doable to fulfil PHP scripts,
without having to change the page. I declare you feature more
about the XML HTTP
Request Object and its capabilities.

The things you crapper do are limitless. For example, you could
create an extremely elegant paging system, that doesn’t require
reloading at all. Or you could create a interface for your PHP
application, which behaves just aforementioned Windows XP. Just think
about it!

Be alive though that JavaScript staleness be enabled for this to
work. Without JavaScript this module be completely useless. So
make trusty your visitors hold JavaScript, or create a
non-JavaScript edition as well.

Comments are closed.