Juarez Posted May 27, 2012 Share Posted May 27, 2012 Hi I'm learning PHP sessions variables. I have a HTML form as below. When I submit the form it passes the entered value as a session variable to processform.php page. (see the code below) This works and I can echo out the variable on the page however when I go to a different page and come back to processform.php the variable has been cleared and I get "Undefined index: name.." on the line where the session variable is set. Session variables should be retained when you come back to the page so what am I missing to do this. I am testing on local host by the way. Many thanks <strong>Test Form</strong> <form method="post" action="processform.php"><br/><br/> <input type="text" name="name" value="testValue" /><br/><br/> <input type="submit" name="submit"/><br/><br/> </form> <?php session_start(); include("menu.php"); echo "<br />"; $_SESSION['name']=$_POST['name']; echo $_SESSION['name']; ?> Link to comment https://forums.phpfreaks.com/topic/263232-session-variable-is-not-retained-when-switching-pages/ Share on other sites More sharing options...
Psycho Posted May 27, 2012 Share Posted May 27, 2012 I would guess that error is because $_POST['name'] is not set. That results in $_SESSION['name'] being overwritten with a NULL value Link to comment https://forums.phpfreaks.com/topic/263232-session-variable-is-not-retained-when-switching-pages/#findComment-1349054 Share on other sites More sharing options...
Barand Posted May 27, 2012 Share Posted May 27, 2012 instead of $_SESSION['name']=$_POST['name']; try if (isset($_POST['name'])) { $_SESSION['name']=$_POST['name']; } Link to comment https://forums.phpfreaks.com/topic/263232-session-variable-is-not-retained-when-switching-pages/#findComment-1349056 Share on other sites More sharing options...
Juarez Posted May 27, 2012 Author Share Posted May 27, 2012 Great thanks, I was wondering whether I should be using 'isset' while searching around. Link to comment https://forums.phpfreaks.com/topic/263232-session-variable-is-not-retained-when-switching-pages/#findComment-1349058 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.