View Full Version : Best to Way to Pass Variables from Ajax to PHP

12-09-2008, 06:14 PM
Is there a “best practice” in passing variables from Ajax to a PHP file?



$constant = “This is my global constant I want accessible in all files.”;


// ajax script
url = "extfile.php";
req.open("GET", url, true);



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.

12-09-2008, 10:17 PM
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).