Reputation: 1615
I'm fairly new to javascript/ajax so bear with me. I'm attempting to send a form field value as a POST variable to a php script, and have that php script set a session variable. I don't need to update any content on the page that I'm calling the javascript script from. So here is what I'm doing...
Here is my javascript stuff:
xmlhttp.open("POST","ajax/create_employee_session_handler.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("blah=1");
window.location="newpage.php";
And here is my php code (simplified):
session_start();
if($_POST)
{
$_SESSION['blah'] = $_POST['blah'];
}
And for some reason, when I redirect to the next page, I get no $_SESSION['blah']. Any suggestions on this would be appreciated.
Upvotes: 0
Views: 575
Reputation: 449415
You are starting the AJAX request, but you are not giving it a chance to finish (The "A" in Ajax stands for "Asynchronous" so when you do the location()
the request is still running.) Chances are the Ajax script never gets called.
Put the part switching the page's location into the Ajax request's success callback to make sure it works out before redirecting.
Upvotes: 4