Forum Moderators: coopster

Message Too Old, No Replies

Passing a session from page A to P B

No way I can understand behavior

         

henry007

10:50 am on Jun 2, 2025 (gmt 0)

Top Contributors Of The Month



In a Q&A each post (for checking purpose) shows its ID, so I know that ID's number is correct
With it I create a session but when it lands on the other page the session id is another one!
<?php
//Unable to pass espected data from P1 to P2
//the deal consists in grabing id_sujet fom P1 and pass it to P2, sounds easy,,, but..?
//P1 is a page from a Q&A script, it shows the result of a pagination script and a LEFT JOIN query that works fine
//id_sujet is extracted as follow:
while ($data = mysqli_fetch_array($result)) {
$id_sujet=$data['id'];//echo $id_sujet;
echo"<a href='insert-reponse2.php'>Answer etc..</a>";
$_SESSION['id_sujet']=$data['id'];
// AS IS EACH POST SHOWS ITS CORRECT ID
//THE SESSION IS VERIFIED BUT DOES NOT CORRECTLY LANDS TO P2, another id is landing?
echo"</td></tr>";
// and more.....
}
// I experienced with: pass the ID via $_GET or as a SESSION
// but it results in an id which is not the correct one
//How can I get what I am looking for?
// Any other way to pass the ID ?

// this is where it should go:
// form section
<?php
SESSION_START();
$id_sujet=$_SESSION['id_sujet']; echo "A.$id_sujet";
//even from page top the id is not the one needed?
//verify if the precedent is killed
//f(isset($_SESSION['id_sujet']))
{
unset($_SESSION['id_sujet']);
}
if(empty($_SESSION['id_sujet']))
{
echo"AAA";
} // yes, no more session
//MySQL conn etc...

// LAST TRY pass it (hiden) to the form and to get it via POST
// but the form is again loaded with wrong ID

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['reponse']) && !empty($_POST['reponse']) && isset($_POST['id_sujet']) )
{
$id_sujet=$_POST['id_sujet'];
echo"AA.$id_sujet";

if (!$id_sujet) {
die("ID sujet manquant.");
}


// Insert response
$sql = "INSERT INTO forum_reponses (auteur, reponse, date_reponse, correspondance_sujet)
VALUES ('$auteur', '$reponse', '$date', '$id_sujet')";
if (!$conn->query($sql)) {
die("Erreur SQL (insert): " . $conn->error);
}

$conn->close();
//header('Location: forum-index.php');
//exit;
}
?>

<form action="insert-reponse2.php" method="post">
<input type="hidden" name="id_sujet" value="<?php echo $_SESSION['id_sujet']; ?>">
// just cheking <?php echo $_SESSION['id_sujet']; ?>
<textarea id="reponse" name="reponse" cols="60" rows="15" placeholder="Votre reponse" required></textarea>
<br><button type="submit">Poster la reponse</button>

henry007

7:11 am on Jun 3, 2025 (gmt 0)

Top Contributors Of The Month



Found a way to do it:
I pass it through GET, then make it a var and insert it as an hiden value in the "post-answer" form and finally use it as $_POST to insert the id corresponding, thus forget about SESSION