Asynchronous Javascript and XML aka AJAX

Discussion in 'JavaScript and AJAX' started by vishal sharma, Jan 16, 2007.

  1. vishal sharma

    vishal sharma New Member

    Joined:
    Jul 23, 2004
    Messages:
    106
    Likes Received:
    6
    Trophy Points:
    0

    A look at how AJAX works



    This tutorial will show you how ajax works and how to use it from a web developers standpoint. Ever wonder how things like G-Talk work? they don't reload the page or use Iframes and yet they always appear to have the data thats coming in instantly. it acomplishes this using ajax. Ajax is not a language of its own, so if you know javascript and HTML, your all set.

    This is where it all comes from, the ability to send and recieve data without reloading the page. First, you need to create an object though,
    Code:
    Firefox: var xmlhttp=new XMLHttpRequest;
    IE: xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    
    The best way I have found to get them both in one command is:
    Code:
    xmlhttp=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
    
    Now you need to create a callable function as well as an onreadystatechange handler. This part is simple. Here's an example:
    Code:
    function getnamedata()
    {
    	xmlhttp.open("POST","file.php");
    	xmlhttp.onreadystatechange=handler;
    	xmlhttp.send("name=random&id=42");
    }
    
    function handler()
    {
    	if(xmlhttp.readyState==4)
    	{
    		alert("Name Info: "+xmlhttp.responseText;
    		xmlhttp.close;
    	}
    }
    
    The xmlhttp.open call opens a connection to the page, the first parameter is POST or GET depending on the method you want to use. The second parameter is the filename of the page relative to the current directory. Line 2 tells it that whenever it goes through a statechange to call a certian function, the handler.

    xmlhttp.send passes variables to the script that you are calling, you'll notice that it passes them in almost exactly the same way as a url bar minus the '?'.

    The handler function gets called every time that xmlhttp changes state. We have the if statement so that it will only execute when readyState==4 (4 is completed). The last statement of course retreives the data from the xmlhttp object. If you are getting most types of data you should use the responseText property, for actual XML data use the responseXML property. The last statement just closes the socket for later use.

    Using this as a basic model you can make an entire website live if you want to, or just find something fun to do with it.
     
  2. rose123

    rose123 New Member

    Joined:
    Jul 14, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi...

    how to access the posted data in file.php??

    i tried like this
    <?php

    $name=$_POST["name"];

    echo $name;

    ?>

    but no luck...
     
  3. evolu

    evolu New Member

    Joined:
    Jul 3, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi i jst want to know can we redirect the user to internet or intranet page based on their internet connectivity

    proced in detail

    i have two type of website hosted in same server one is for internet user and other is for intranet user i have to take the user to internet page if he is accessing the site with internet connection r else he have to go to intranet site (people who dosen't have internet connection )
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice