SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    CA
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do i pass some variables to a script with XMLHttpRequest?

    I have this function that executes whenever the page is loaded ( <body onload = 'visitorLog()'> ) and this function calls and executes the php script in visitor.php.. question is, how do i sent to that script some variables... like for example the URL?

    PHP Code:
    <script language="javascript" type="text/javascript">

    function 
    visitorLog(){

        var 
    requestObject "false";

        if(
    window.XMLHttpRequest){
        
    requestObject = new XMLHttpRequest();
        }else if (
    window.ActiveXObject) {
        
    requestObject = new ActiveXObject("Microsoft.XMLHTTP");
        }

        if (
    requestObject){
        
    requestObject.open("GET""visitor.php");
        
    requestObject.send(null);
        }
        
    }

    </script> 

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    CA
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never mind, i got it! Learning this stuff is awsome!!

    PHP Code:
    <script language="javascript" type="text/javascript">

    function 
    visitorLog(){

        var 
    requestObject "false";

        if(
    window.XMLHttpRequest){
        
    requestObject = new XMLHttpRequest();
        }else if (
    window.ActiveXObject) {
        
    requestObject = new ActiveXObject("Microsoft.XMLHTTP");
        }

        var 
    myURL document.URL;   // added this line
        
    if (requestObject){
        
    requestObject.open("GET""visitor.php?url=" myURL);  // modified this line
        
    requestObject.send(null);
        }
        
    }

    </script> 

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are just learning ajax. Might I suggest CPAINT. It will save you a lot of headaches in terms of passing special characters and parsing XML (it gives you the option to abstract the XML parsing and just have it dump the returned xml into one javascript object which you can easily access.)

    cpaint.wiley14.com

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    CA
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeap, i'm just learning it, and i love it. Thank you for the suggestion, i'll check it out... i just had some trouble with a special character, the & that showed up in some url-s... i just did a:
    myURL.replace('&', '_');

    it works... there's probably nicer ways to deal with that but so far so good.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •