Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

    Passing Javascript var to PHP without redirection

    I'm pretty sure this is possible with AJAX.

    Also, please respond to this, even if its just a link to a tut. This would solve a lot of problems for me!

    How do I pass a javascript variable to PHP WITHOUT REDIRECTION

    Is there an AJAX solution?

    ty!

    ~binaryWeapon

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    That's exactly what AJAX is, an HTTP Request sent to the server via Javascript without the browser reloading the page. Google ajax +tutorial to learn the basics.

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Oh ... so thats what its for .... ;-)

    Code:
    function callback(serverData, serverStatus) {       // Called automatically when we get data back from server
       alert(serverData);                               // Display an alert box with the recieved data
    }
    
    function ajaxRequest() {
       var AJAX = null;                                 // Initialize the AJAX variable.
       if (window.XMLHttpRequest) {                     // Does this browser have an XMLHttpRequest object?
          AJAX=new XMLHttpRequest();                    // Yes -- initialize it.
       } else {                                         // No, try to initialize it IE style
          AJAX=new ActiveXObject("Microsoft.XMLHTTP");  //  Wheee, ActiveX, how do we format c: again?
       }                                                // End setup Ajax.
       if (AJAX==null) {                                // If we couldn't initialize Ajax...
          alert("Your browser doesn't support AJAX.");  // Sorry msg.                                               
          return false                                  // Return false, couldn't set up ajax
       }
       AJAX.onreadystatechange = function() {                      // When the browser has the request info..
          if (AJAX.readyState==4 || AJAX.readyState=="complete") { //  see if the complete flag is set.
             callback(AJAX.responseText, AJAX.status);             // Pass the response to our processing function
          }                                                        // End Ajax readystate check.
       }
       var url='http://somedomain.com/getdata.php'; // This is the URL we will call.
       AJAX.open("GET", url, true);                                  // Open the url this object was set-up with.
       AJAX.send(myVariable);                                              // Send the request.
    }
    Here's what I've found (on a tutorial). Would this work? And what PHP would I need?
    Last edited by binaryWeapon; 11-05-2007 at 03:22 PM.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Yeah, just add the variables to the URL.
    Code:
    var url = 'http://somedomain.com/getdata.php?variable=1&variable2=2';
    And you would access them in your PHP page by using the $_GET superglobal array:
    PHP Code:
    $var $_GET['variable'];
    $var2 $_GET['variable2']; 

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    OK so where it says
    Code:
       AJAX.send(myVariable);
    I can just send null? (That's what it had in the tutorial, I just changed it 'cause I thought that's what would send the var.)

    So it would be
    Code:
       AJAX.send(null);

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Yes, I believe you can. Why don't you try it and find out for yourself?

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    willdo! thx for ur help!


  •  

    Posting Permissions

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