Output HTML INPUT 'array'

Discussion in 'PHP' started by Omzy, Dec 21, 2008.

  1. #1
    I have the following checkboxes on Form 1:

    foreach ($cat as $index => $value)
    {
    <input type="checkbox" name="selection[]" value="'.$cat[$index][2].'" id="'.$index.'"/>
    }
    Code (markup):
    On Form 2, I use the following code to display the values of the checkboxes that have been selected:

    foreach($_POST['selection'] as $key=>$value)
    { 
    	echo '.$value.';
    }
    Code (markup):
    I now need to repeat this on Form 3. I understand I would need to put these as hidden form variables, which I have tried doing but to no avail. Can anyone help?
     
    Omzy, Dec 21, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You want to keep the checkbox values while having them go through different forms? I'd recommend using session vars which means at the top of your PHP, before ANYTHING is output to the page, put session_start(); and then do like $_SESSION['selection'] = $_POST['selection']; then you can access your $_SESSION['selection'] on the next page loaded so long as you do the session_start(); again at the very top of the page.

    If you don't want to do that, then you'd just have to do your hidden input idea like:

    foreach($_POST['selection'] as $key=>$value)
    {
    	?><input type="hidden" name="selection[]" value="<?=$value?>" /><?
    }
    PHP:
     
    zerxer, Dec 21, 2008 IP