Articles

PHP Checkboxes and Arrays

May 5, 2009 at 09:25:31
Specs: Windows Vista

Using a PHP form, a user selects multiple checkboxes (not required fields) and forgets to enter name etc(required field), the form submits & reloads a duplicate form returning previous entered values but not for the checkboxes. How can I list all the checkboxes from the array and keeping the ones ticked that the user checked before the form was posted? Many thanks for any help in advance.

See More: PHP Checkboxes and Arrays

Report •


#1
May 5, 2009 at 09:35:21

I'm new to PHP so find this difficult but here is some of the code I've been playing with:

[code]
<input type="checkbox" value="cbcontact" name="features[]" />
<input type="checkbox" value="cbexternal" name="features[]" />

OR

// Generating the checkboxes
$choices = array('cbcontact' => 'Contact Form')',
'cbexternal' => 'External Links')',
'cbnews' => 'News Section');
foreach ($choices as $key => $choice) {
echo "<input type='checkbox' name='optfeatures[]' value='$key'/> $choice \n <br />";
[/code]

Once again any help would be greatful i'm truely lost. (I'm not using a database here at all, the form will be emailed to myself which i've just about finished)


Report •

#2
May 6, 2009 at 13:21:31

The arrays would only contain elements that were checked, so if you have 5 options, but the user only checks the 5th, then you'll get an array of one element belonging to the 5th option.

In your arrays, you might want to name the index for each option, so that you can test it directly to see whether or not it exists.

<input name="features['cbcontact']" value="cbcontact" type="checkbox">

if(isset($_POST["features"]["cbcontact"])) { ... }

Another way for grins, might be something like

<?php

   global $cboxes;

   if(isset($_POST["cmd"]) && $_POST["cmd"] === "test") {

      if(empty($_POST["req"])) {
         $cboxes = "";
         if(count($_POST["cb"]) > 0) { $cboxes = implode(chr(32), $_POST["cb"]); } ?>

         <form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
            cbox1 <input name="cb[]" value="check1" type="checkbox" <?php
               echo (preg_match('/check1/i', $cboxes) > 0 ? " checked" : "");?>>             
            cbox2 <input name="cb[]" value="check2" type="checkbox"<?php
               echo (preg_match('/check2/i', $cboxes) > 0 ? " checked" : "");?>>
            cbox3 <input name="cb[]" value="check3" type="checkbox"<?php
               echo (preg_match('/check3/i', $cboxes) > 0 ? " checked" : "");?>>
            cbox4 <input name="cb[]" value="check4" type="checkbox"<?php
               echo (preg_match('/check4/i', $cboxes) > 0 ? " checked" : "");?>> 
            cbox5 <input name="cb[]" value="check5" type="checkbox"<?php
               echo (preg_match('/check5/i', $cboxes) > 0 ? " checked" : "");?>> 
            * req <input name="req" value="" type="text">
            <input name="cmd" value="test" type="submit">
         </form> <?php     
      } else {
         // mail form
      }

   } else {
?>

<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
cbox1 <input name="cb[]" value="check1" type="checkbox">             
cbox2 <input name="cb[]" value="check2" type="checkbox">
cbox3 <input name="cb[]" value="check3" type="checkbox">
cbox4 <input name="cb[]" value="check4" type="checkbox">
cbox5 <input name="cb[]" value="check5" type="checkbox">
   * req <input name="req" value="" type="text">
<input name="cmd" value="test" type="submit">
</form>
<?php } ?> 

Not really tested, but it *may* work.

Hope that helps.

______________________
My work in progress. I hate JS. :P


Report •

#3
May 7, 2009 at 17:11:45

Hi shutat,

Excellent thanks for your quick repsonse, I'll have a play with your ideas and see if I can get them to work!

Thanks,

Chris


Report •

Related Solutions


Ask Question