View Full Version : Passing Javascript var to PHP without redirection

11-05-2007, 04:31 AM
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?



11-05-2007, 05:10 AM
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.

11-05-2007, 02:53 PM
Oh ... so thats what its for .... ;-)

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?

11-05-2007, 05:37 PM
Yeah, just add the variables to the URL.
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:

$var = $_GET['variable'];
$var2 = $_GET['variable2'];

11-06-2007, 04:31 AM
OK so where it says

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


11-06-2007, 04:39 AM
Yes, I believe you can. Why don't you try it and find out for yourself? ;)

11-06-2007, 04:43 AM
willdo! thx for ur help!