-2

.

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

Saif Obeidat
  • 128
  • 1
  • 2
  • 16

1 Answers1

0

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();
wuno
  • 9,547
  • 19
  • 96
  • 180
  • thanks .. but i need to print the values of checkboxes when i click the print button , – Saif Obeidat Dec 25 '15 at 07:39
  • ok print button on the second page? – wuno Dec 25 '15 at 07:40
  • I updated so when you click print it takes your to a print page. Also I showed you how to store the variables in a session so you can use them again. – wuno Dec 25 '15 at 07:45
  • thanks for your effort with me .. i used the session before like this if(isset($_POST['do'])) { $mid = $_POST['mid']; $dis = $_POST['dis']; $_SESSION['mid'] =$mid; $_SESSION['dis'] =$dis; } if(isset($_POST['print'])) { foreach($_SESSION['mid'] as $values) echo $values ; } but the same error Undefined variable: _SESSION – Saif Obeidat Dec 25 '15 at 07:56
  • why the code looks iike this way ! do you have facebook account ? :/ – Saif Obeidat Dec 25 '15 at 07:59
  • Add this to the top of the page session_start(); – wuno Dec 25 '15 at 07:59