meWantToLearn meWantToLearn - 5 months ago 22
HTML Question

session gets lost after form submit

When a user links to link it redirects to edit.php - here's an example: www.cars.com/edit.php?id=23

In edit.php, I use

_GET
to store the value in a session. The value is stored in
$_session['user']
but when the form on the same page is submitted
echo $_session['user']
displays nothing - how can I make it display the value?.

<?php
session_start();
$_session['user']=$_GET['id']; // I use _GET to store the value in session
if( isset($_POST['submit'])) {
echo $_session['user'];

}

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="formI2D" enctype="multipart/form-data" id="formI2D" />




Answer

It's because you're redeclaring your $_SESSION['user'] even when it's a POST (I think).

You can fix this by adding ?id=$_GET['id'] in you form's action, or by wrapping your $_SESSION initialisation like that:

if (isset($_GET['id'])) {
     $_SESSION['user']=$_GET['id'];
}

Also, you should use uppercase for php global arrays ($_POST, $_COOKIE, $_SESSION etc)