Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Photoshop Cafe XeonFX Photoshop Lab Ray-Industries 

Change Language

Who's online

There are currently 0 users and 6 guests online.

The very basics of AJAX

In this tutorial I will show you the really basic of AJAX and make a script that just returns the IP-address of the person viewing the site...

For those who didn't know, AJAX is done in JavaScript (or at least I've always done it there). I've made a function witch does make things a bit easier... Let's take a look at that one:

  1. function getXHttpElement(){
  2. var xmlHttp;
  3. try {
  4. xmlHttp=new XMLHttpRequest();
  5. } catch (e) {
  6. try {
  7. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  8. } catch (e) {
  9. try {
  10. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  11. } catch (e) {
  12. alert("Your browser does not support AJAX!");
  13. }
  14. }
  15. }
  16. return xmlHttp;
  17. }

Now this might look confusing, but all it does is to create a new AJAX object and return it. The complicated code is just because some browsers uses different methods, so this should work on most browsers... If it don't, JavaScript will alert the user.

Now what this does is as said just to return a AJAX object. It doesn't do anything more then that...

But we want it to do a little bit more so let's continue coding, shall we?

  1. AJAX_object = getXHttpElement();
  2. AJAX_object.onreadystatechange=function(){
  3. switch(AJAX_object.readyState){
  4. case 0:
  5. alert("State 0");
  6. break;
  7. case 1:
  8. alert("State 1");
  9. break;
  10. case 2:
  11. alert("State 2");
  12. break;
  13. case 3:
  14. alert("State 3");
  15. break;
  16. case 4:
  17. alert("State 4");
  18. break;
  19. }
  20. }

Wow, wow, wow, you might say. That's an awful lot of code... I know it is but it's really simple... Here we create a AJAX-object named AJAX_object with the function we created... Then we say AJAX_object.onreadystatechange...

AJAX objects have so called readystates... This tells where in the process the object is... When you create a AJAX-object it enters state 1... This isn't something you need to remember but I thought I'd tell you... The only state you should care about is state 4. That's when AJAX has received the answer to the call to the server. So let's say we have a phpfile named getIp.php (this is really simple to create, the code is only:

  1. <?php
  2. echo $_SERVER['REMOTE_ADDR'];
  3. ?>

).

So we want to get the IP to the viewer... Well, to do that we simply change the state 4 to:

  1. alert(AJAX_object.responseText);

Then we set up the AJAX-object to get ready to send with the code:

  1. AJAX_object.open("GET","getIp.php",true);

This set's up the AJAX_object to send and receive data to/from getIp.php. Then we only need one command left and that is:

  1. AJAX_object.send();

And that's it... HTH


Submitted by Alxandr on March 27, 2007 - 1:18pm.
printer friendly version

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati