Computing.Net > Forums > Web Development > PHP Checkboxes and Arrays

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP Checkboxes and Arrays

Reply to Message Icon

Name: chris890
Date: May 5, 2009 at 09:25:31 Pacific
OS: Windows Vista
Subcategory: PHP
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: chris890
Date: May 5, 2009 at 09:35:21 Pacific
Reply:

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)


0

Response Number 2
Name: shutat
Date: May 6, 2009 at 13:21:31 Pacific
Reply:

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


0

Response Number 3
Name: chris890
Date: May 7, 2009 at 17:11:45 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP Checkboxes and Arrays

Help with Checkboxes and array PHP www.computing.net/answers/webdevel/help-with-checkboxes-and-array-php/2568.html

php checkboxes www.computing.net/answers/webdevel/php-checkboxes/2390.html

PHP/MySQL and CRM help! www.computing.net/answers/webdevel/phpmysql-and-crm-help/2520.html