Reputation: 57
I'm using an Ajax call to pass ID to a small php file that just updates a session variable (ID). I then open a new tab using another php file that populates the page with info particular to that ID by referencing it in MySQL. The ajax call is successful. The network traffic shows code 200. I then open the new page in the new tab. But the ID sent never made it to the $_POST['ID'] array element.
The ajax code is triggered by a click event that captures the curr ID value . .
var pkg = {ID:ID};
$.ajax({
type: "POST",
url: "../php-main/updateSessionIDVar.php",
data: pkg,
success: console.log("ID value from ajax post:" + ID), // this shows OK
error:function(jqXHR, textStatus, errorThrown){
console.log("Error type" + textStatus + "occured,
with value " + errorThrown)
}
});
window.open('partEdit.php','_blank');
updateSessionIDVar.php file . .
<?php
if (session_status() == PHP_SESSION_NONE) session_start();
if (isset($_POST['ID'])) { //this test always fails
$ID=$_SESSION['currID']=$_POST['ID'];
}
else $_SESSION['currID']='63'; // Dummy val so I can test the rest of my code
$user=$_SESSION['userID'];
?>
Start of php file that makes new page "partEdit.php" . .
<?php if (session_status() == PHP_SESSION_NONE) session_start();
$ID=$_SESSION['currID'] ;
$user=$_SESSION['userID'];
?>
<html> etc.
I've read several other SO related threads but none seem to fit. Any advice appreciated.
Upvotes: 3
Views: 1988
Reputation: 446
Everything you have done seems fine. Try refreshing the page if any of the session variables change. So that cache/loaded things fresh up.
Upvotes: 0
Reputation: 57
After re-reading several related questions I dropped the Ajax call altogether and tried to send the var partID in the body of a window.open function. Like this:
window.open('partEdit.php?partID='+partID, target='_blank');
Using this strategy, in the partEdit.php file I was able to access the $_GET array to find the value of partID.
$partID=$_GET['partID'];
This did the job although I still don't understand why posting the value of partID using an Ajax call did not work. I think my problem lies with clearly understanding how the $_POST[] var gets set and cleared during a 'session' where there can be several domain tabs available and the user can mouse click between them and open new tabs (or windows) as well.
I'm afraid I don't even know how to ask the question right now. But if anyone knows where I can find a good reference that explains this area of data transfer between the server and the active window in detail please put the link (or book title) in a comment. I have found no clear explanation of this after many days now.
Upvotes: 1