Forum Topic: Php: Ajax

(1,042 views • 14 replies)

This topic is 1 page long.

<< < > >>
None

SpamBurger

Reply To Post Reply & Quote

Posted at: 11/18/06 05:39 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Php: Main

Welcome to my AJAX tutorial. If you don't know already, this tutorial will be teaching you the basics of the AJAX library of javascript, and how to use it to work with PHP scripts. I know AJAX isn't PHP, but it does relate to it because it can be used to run PHP scripts. Also, I would like to say that I am also pretty new to AJAX myself so I will be only teaching the basic through intermediate stuff. There is more advanced things you can do, but I will leave that for another tutorial. So, on with the lesson!
------------
What is AJAX?

AJAX stands for Asynchronous Javascript And XML. The beauty of AJAX is that it can be used to communicate with the server, but not have to reload the whole entire page. The only bad thing I can think of about it is that it doesn't work well in safari (that's just what I have read) and doesn't work in most old browsers. So if you are interested in AJAX, read ahead.
------------
Starting with AJAX

Before you can start working with AJAX, you must start a connection by one simple function. Unfortunately, this function isn't the same for all browsers, so you will need to use try and/or if statements to declare the right one for each browser. Throughout this tutorial, I will be using the firefox function (which is also used for most other browsers), but I will list the other function as well (the IE one). The two functions are below:

Firefox: new XMLHttpRequest()
IE: new ActiveXObject("Microsoft.XMLHTTP")

Now that you know how to start a connection, we can begin working with it.
------------
Make a new html file, and copy the following into it:

<script type="text/javascript">
var ajax=new XMLHttpRequest();
</script>

That script opens up a new connection. Since we now have the connection open, we must declare a page to use. To do that, we use the open() method. This function accepts three arguments. In the first argument, you state how the data is to be transmitted. That means you enter either GET or POST (I assume you know what those are). In the second argument, you state the url to the page you will be working with. So in this tutorial, we will be using the url ajax.php. In the third argument, you state whether or not the request is asynchronous. Just set it to true and you will be fine. You also need to send some data along with the request. To do this, you use the send() method which accepts one argument, the string of data to be sent. If you are using GET, you would just set this argument to null. So now that you know all of that, lets do an example script that opens up the page ajax.php.

<script type="text/javascript">
var ajax=new XMLHttpRequest();
ajax.open("GET","ajax.php",true);
ajax.send(null);
</script>

As of right now, that script is useless because we can't do anything so far. Oh, and if you want to pass GET data through the url, you would do like if it was the address in a page. So, you would do:

ajax.open("GET","ajax.php?var=value",true);

instead of what I said before. Anyway, now I will teach you how to actually get the text returned from the server.
------------
Storing the Returned Text

To start working with the returned text from the server, we need to declare the property that processes the response from the server. This property is onreadystatechange and is used like so:

ajax.onreadystatechange=function(){
//some js code
}

Now that we have that done, we just need to use one little property that stores all the returned text from the server. That property is responseText. But before we use it, we need to first declare when the server text should be returned. To do that, we use the readyState property. This property will have a value from 0 to 4. Depending on which one you use, determines when the text will be returned. Below tell you when to use each one copied directly from w3schools:

readyState=0 after you have created the XMLHttpRequest object, but before you have called the open() method.

readyState=1 after you have called the open() method, but before you have called send().

readyState=2 after you have called send().

readyState=3 after the browser has established a communication with the server, but before the server has completed the response.

readyState=4 after the request has been completed, and the response data have been completely received from the server.

Four is the one you will be using the most. Since you have learned all that, below is the example script that outputs the text from ajax.php:

<script type="text/javascript">
var ajax=new XMLHttpRequest();

ajax.onreadystatechange=function(){
if(ajax.readyState==4){
document.write(ajax.responseText);
}
}

ajax.open("GET","ajax.php",true);
ajax.send(null);
</script>

If we put the following code in ajax.php:

<?php
echo "Hello World";
?>

and you go to the html file with this script, you should see the text Hello World outputted on the page. This tutorial continues onto the next post.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 11/18/06 05:40 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Sending POST Data

Sending POST data with AJAX is a bit different than sending GET data. The only main difference is that you need to set a header on how the data is to be encoded. To do this, you would use the setRequestHeader() method. There are also the other little differences such as in the open() method, you use POST instead of GET, and you actually have to set data in the send() method. Below is a script that will send POST data:

<script type="text/javascript">
var ajax=new XMLHttpRequest();
ajax.open("POST","ajax.php",true);
ajax.setRequestHeader("Content-Type","applica tion/x-www-form-urlencoded");
ajax.send("var1=value1&var2=value2");
</script>

And to output the returned text, you would do it the same way as in the GET example. Also, you should make sure you url encode the string in the send() method. I forgot the javascript function for doing this, but you can look it up yourself.
------------
Well, that is the end of my tutorial. I hope you learned something. Sorry if this isn't that great, I wrote it fairly quickly. I just wanted to get the basics of AJAX across.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

thecoshman

Reply To Post Reply & Quote

Posted at: 11/18/06 05:43 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

wow dood, this look great.

yet to read it in details, but it seems to perfect for any one who wants to start using AJAX, or is not sure if they want to start using it.

good work dood! would be nice to see AJAX covered in a lot more detail, epsecialy somthing that says what it is realy clearly. did you say that it is a JS libary?


None

Mister-Mind

Reply To Post Reply & Quote

Posted at: 11/18/06 05:47 PM

Mister-Mind EVIL LEVEL 07

Sign-Up: 07/01/06

Posts: 2,196

Very Hawt!

Jks mate!

Its a Great tutorial. Aspecially for AJAX dummies like me =P


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 11/18/06 05:47 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 11/18/06 05:43 PM, thecoshman wrote: good work dood!

Thanks :)

did you say that it is a JS libary?

Yes I did in this sentence:

If you don't know already, this tutorial will be teaching you the basics of the AJAX library of javascript, and how to use it to work with PHP scripts.

Also, DFox gave me the idea of explaining how to get around the cache in IE. So expect me to post that in a few minutes.

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

Jordan

Reply To Post Reply & Quote

Posted at: 11/18/06 05:50 PM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,833

A good tutorial for Ajax beginners. :)

Beware of IE, to anybody attempting ajax.

None

SpamBurger

Reply To Post Reply & Quote

Posted at: 11/18/06 05:53 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

Cache Busting in IE

IE for some reason likes to cache the page when calling it. Fortunately, this is very easy to get around. You simply need to create a unique identifier to pass along in the url so IE is forced to update. To do this, you would do the following:

<script type="text/javascript">
var ajax=new ActiveXObject("Microsoft.XMLHTTP");

var sid=Math.round(Math.random()*1000);

ajax.open("GET","ajax.php?sid="+sid,true);
ajax.send(null);
</script>

What that does is simply pass along a random number to the url so IE is just forced to update. Very simple, isn't it?

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


None

thecoshman

Reply To Post Reply & Quote

Posted at: 11/18/06 05:59 PM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

ok, so when i said 'did you say AJAX was a JS libary', i realy was not expecting such a witty reply.

I never knew that AJAX was a special libry, rather just a special way of using it all. does any one know what the current sort of percent of net faring people is that have JS enabled? after all, not much point to all this if no one is using it.

But yer dood, keep up to good work!


None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 11/18/06 11:08 PM

Pilot-Doofy NEUTRAL LEVEL 37

Sign-Up: 09/13/03

Posts: 12,275

This doesn't really tie in with PHP at all, but nice tutorial either way.


None

DannyIsOnFire

Reply To Post Reply & Quote

Posted at: 11/19/06 04:12 AM

DannyIsOnFire DARK LEVEL 19

Sign-Up: 04/14/05

Posts: 7,191

At 11/18/06 11:08 PM, Pilot-Doofy wrote: This doesn't really tie in with PHP at all, but nice tutorial either way.

I thought that as well =P
Still, a nice tutorial to have, and could be used in conjunction with PHP so... :)


None

Jordan

Reply To Post Reply & Quote

Posted at: 11/19/06 04:18 AM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,833

At 11/18/06 11:08 PM, Pilot-Doofy wrote: This doesn't really tie in with PHP at all, but nice tutorial either way.

What do you mean?

At 11/18/06 05:39 PM, SpamBurger wrote: <?php
echo "Hello World";
?>

But yes, i agree. Ajax isn't directly associated with php. But it is still nice to have an ajax tutorial - So that when people ask for stuff that we usally reply to "You would need ajax for that" we will actually have an ajax tutorial to give them.


None

elbekko

Reply To Post Reply & Quote

Posted at: 11/19/06 07:48 AM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

The POST thing really helped, I was about to go on a Googling hunt for info on that :D

Great :D

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

NinoGrounds

Reply To Post Reply & Quote

Posted at: 12/14/06 04:16 PM

NinoGrounds EVIL LEVEL 17

Sign-Up: 11/28/05

Posts: 3,756


None

Pilot-Doofy

Reply To Post Reply & Quote

Posted at: 7/21/07 11:51 AM

Pilot-Doofy NEUTRAL LEVEL 37

Sign-Up: 09/13/03

Posts: 12,275

Sorry to bring news to bad practice, but I was looking over this tutorial again today. I just realized that there's no exception handling for this. This isn't a very portable script.


None

SpamBurger

Reply To Post Reply & Quote

Posted at: 7/21/07 12:06 PM

SpamBurger NEUTRAL LEVEL 15

Sign-Up: 07/12/05

Posts: 4,747

At 7/21/07 11:51 AM, Pilot-Doofy wrote: Sorry to bring news to bad practice, but I was looking over this tutorial again today. I just realized that there's no exception handling for this. This isn't a very portable script.

Fine!

Just throw in some try...catch blocks, its not very hard >:(

"However, the game received only two orders, one of which Molyneux speculated was from his mother." -Peter Molyneux's first game The Entrepreneur


All times are Eastern Standard Time (GMT -5) | Current Time: 05:17 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!