I’m playing with small exercise to understand sessions and posts, but can’t get them to behave together. Can you help?
Situation
I have the following pages
- Page 1
- Page 2
On page 1 I have a single input form. The input is a checkbox, ex:
<input type="checkbox" name="test" >
-
I want to be able to arrive at page 1 without receiving errors
-
I would like the unselected state of the checkbox to have a
value of 0, and the selected state avalue of 1 -
Whatever value is assigned (by way of the checkbox), I would like that value stored in a session that can travel from page 1 to page 2 and back.
-
Once a session value is stored, I would like to select or deselect the checkbox on page 1 to change the value, allowing me to travel from page 1 to page 2 and back with the new value.
Here is my code for this (which does not work):
Page 1
Note: I have this code on the same page as the form. The checkbox name="test"
if (isset($_POST['test']))
{
$_SESSION['test'] = $_POST['test'];
}
if (empty($_SESSION['test']))
{
$_SESSION['test'] = 0;
}
else
{
$_SESSION['test'] = 1;
}
var_dump($_SESSION['test']);
Page 2
var_dump($_SESSION['test']);
Thanks in advance for you help.
UPDATE
If you would like to see the full code for both pages, I have posted them here:
Page 1:
http://www.diigo.com/item/note/17zt6/ux45
Page 2:
Use this code for your page1.php