Keep Checkboxes Checked Upon From Submit

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

  1. #1
    Below is the code for my checkboxes, I have got a FOREACH loop which creates all the checkboxes.

    foreach ($cat as $index => $value)
    {
    echo '<input type="checkbox" name="selection[]" value="'.$cat[$index][2].'" id="'.$index.'"/>'.$cat[$index][2].';
    }
    Code (markup):
    I've got some form processing going on that counts the number of checkboxes that are checked and if it is less than 3 then it redisplays the form with an error message. But I would like it to keep the checkboxes ticked too - how can this be acheived with the above code setup?
     
    Omzy, Dec 20, 2008 IP
  2. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #2
    try this
    foreach ($cat as $index => $value)
    {
    	$selected = ($_REQUEST['selection'][$index]) ? ' checked="checked"':'';
    	echo '<input type="checkbox" name="selection[]" value="'.$cat[$index][2].$selected.'" id="'.$index.'"/>'.$cat[$index][2];
    }
    PHP:
     
    misbah, Dec 20, 2008 IP
  3. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Unfortunately that didn't work. I am using the form method POST if that's any more help...
     
    Omzy, Dec 21, 2008 IP
  4. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Anybody have any ideas?
     
    Omzy, Dec 22, 2008 IP
  5. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I've not used foreach when using lots of checkboxes - I number them instead with the id from the table in the database and check them afterwards with a while() loop.

    Build your checkboxes with a number on the end like this:
    <input type="checkbox" name="chk1" />
    <input type="checkbox" name="chk2" />
    <input type="checkbox" name="chk3" />
    Code (markup):
    and so-on.

    Next use something like this to rebuild them:
    $row=mysql_query("SELECT data FROM table");
    while ($row=mysql_fetch_assoc($query)) {
      echo '<input type="chechkbox" name="chk'.$row['id'].'"'.($_POST['chk'.$row['id']] ? ' checked="checked"' : '').' />';
    }
    PHP:
     
    Yesideez, Dec 22, 2008 IP
  6. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi Yesideez,

    I'm not using a database, in fact I'm storing the menu data in an array $cat.

    Do u know how I can implement your solution using the variables I have used in my code above?
     
    Omzy, Dec 22, 2008 IP
  7. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $arrSize=count($cat);
    for ($i=0;$i<$arrSize;$i++) {
       echo '<input type="checkbox" name="chk'.$i.'"'.($_POST['chk'.$i] ? ' checked="checked"' : '').' />';
    }
    PHP:
    Try that and see how you go.
     
    Yesideez, Dec 22, 2008 IP