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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Best to Way to Pass Variables from Ajax to PHP

    Is there a “best practice” in passing variables from Ajax to a PHP file?

    Ex:
    index.php
    Code:
    <?php 
    
    $constant = “This is my global constant I want accessible in all files.”;
    
    ?>
    
    // ajax script
    	url = "extfile.php";
    	req.open("GET", url, true);
    exitfile.php
    Code:
    <?php
    
    echo $constant;
    
    ?>
    This will work using sessions or if I pass $constant through the URL, but as I’m new to Ajax I’m wondering if there’s a better, more appropriate way of handling this. Thanks.

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    If I understand your needs correctly, that's fine.

    Don't forget to send() your request though.

    And you can grab that constant variable via .responseText (you'll need an onreadystatechange even handler to detect when you've received your response).
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com


  •  

    Posting Permissions

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