This question already has an answer here:
.
in my project i have two pages , first page has a form (check-boxes) , and the second page will print the values of the checked boxes when i click submit/print button .
if( isset($_POST['do']))
{
$mid = $_POST['mid'];
}
once i click the " do button " in the first page , i go to the second page , then take the checked box values by $_POST['mid']
and when i click on the print button it should print the values of the checked box
if(isset($_POST['print']))
{
foreach($mid as $values)
echo $values;
}
but it gives me :
Undefined variable: mid ., and Invalid argument supplied for foreach()
notice that it works when i put the foreach statement in the first -if- : if( isset($_POST['do'])) , but i dont want it be like that.
and thank you all
</div>
If you are posting to a third page you must pass the variable to that page as well. So from your html form you post the variable which is saved with the name
of the input type. We will assign the name of the input types value to a variable on the second page and then echo it to the screen.
Html Form
<form action="myPhpPage.php" method="post">
<input type="checkbox" name="gender" value="Male">Male</input>
<input type="checkbox" name="gender" value="Female">Female</input>
<input type="submit" name="submit" value="Submit"/>
</form>
myPhpPage.php
<?php
// turn the check boxed gender to a variable called $myGender
$gender = $_POST['gender'];
// now print your variable to the screen
// there is no reason to move to a new page for this,
//if you do go to a new page then you need to pass the variable to the // //new page as well.
echo $gender;
$_SESSION['gender'] = $gender;
<input type="button" value="Print" href="printPage.php" />
?>
printPage.php
$gender = $_SESSION['gender'];
echo $gender;
Add this to the top of all your pages that you want the session
session_start();