it became boring to input
data and then press that damn "Submit" button over and over everytime we want to
add or look for something!
but we are not going to stay there accepting this anymore, are we? :)
NOTE: this articles assumes that you have basic
knowladge of javascript and PHP.
in this tutorial, we will be looking at the revolutionary XMLHttpRequest object
which allows us to send and receive data dynamically without the need of
refreshing the page.
This technology was first introduced by Microsoft in Internet Explorer 5 as an
ActiveX branch then FireFox 1.0, Netscape 7 and Safari 1.2 followed.
To instantiate an XMLHttpRequest object you have to write this for IE users:
|
For other compatible browsers:
|
the XMLHttpRequest object has the following methods:
We will be using open() and send() method all the time mainly!
the open() method takes 5 arguments 3 of them are optional.
the first 2 arguments are the method (GET or POST) and the URL to which you are
going to send the request.
The send() method will, obviously, send the request.
Let's look at this example to clear things up.
Let's assume we have a phonebook and all data are stored in a MySQL table and we
want to make a search page where the user enters a number and the script should
check if the number exists in the database or not.
If the number was found, will display the name of our contact otherwise we will
show "Not found!" message.
all that without even reloading the page after submition!
So first let's take a look at the table structure (let's name numbers):
|
We have created a table with 2 fields: cells and
name
"cells" as a primary key.
Now let's take a look at our magic function which will fetch the data without
refreshing:
|
We have defined a function which take 2 arguments: url and number
the "url" argument will take the URL to which we will send the request and the
"number" argument is the number we are looking for.
we also want to prevent the script from sending a new request if the number was
the same as before! so we defined the variable oldData which will save the last
number entered and compare it to the new number entered to the form if it was
the same, it will quit and will not send a new request:
|
we also don't want to send a request if the number was empty!
but what about this doesNotSupport variable? the first time the script runs it
will hold "true" and then try to send the request! if the XMLHttpRequest object
was not found or couldn't be instantiated, we will alert the user and set the
value to "false" in:
|
so if the user tried to enter a new number to force the request to be resent
it will refuse to bother trying to send the request again :p
if the user could pass the condition above, then we assign the current number to
be queried to the variable oldData and display the word "Search..." to make it
look dynamic:
|
The onreadystatechange property is an event handler which fires a function
when the the state changes:
|
We declared that when the state changes, processReqChange() function should
be invoked. We will see the definition of this function later.
It's worth saying that when a request is sent these properties are populated:
readyState holds the object status (as integer):
now let's take a look at the processReqChange() function definition:
|
This function will check if the readyState is 4 (completed) and status is
"200" (OK) to display the result in the field "result".
Here is the form we are using:
|
The result field will invoke the function getNumber() when ever it loses fox
and it passes the URL (getnumber.php) and its value (this.value)
Here is how our getnumber.php looks:
|
This simple script will take the number from the URL (i.e.
getnumber.php?number=5454014) and then checks it in the database.
if the name was not found, we will print "Not found!" otherwise we print the
name.
Our javascript function will take what has been printed (the answer to the
request) and put it in responseText property. You can however, use the
responseXML property when your answer to the request is XML.
That's all!
you can view the script live at: XMLHTTPRequest.htm
NOTE: in order to see this script in action, you
have to enable javascript and your browser should be one of the compatible
browsers with XMLHttpRequest technology.
try to input the number 123456789 and you should see "from1To9" in the result
field. Any other number will display Not found!
* Many details were inspired and taken from: http://developer.apple.com/internet/webcontent/xmlhttpreq.html
Saleh Jamal
Article posted in
http://forum.worldofphp.com in May 20, 2005
[http://www.phpsimplicity.com]