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']; ?> Quote Link to comment 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 Quote Link to comment 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']; } Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.