Jump to content

PHP Checkbox Array


Ryukotsusei

Recommended Posts

I have a form with a set of checkboxes that I would like to verify with PHP. I want the PHP to display one message for the ones that are checked, and a different message if they're not. However, the code seems to be labeling them all as checked for some reason. It can't seem to identify when they're not.

 

Checkbox in the form:

<input type='checkbox' name='codes[]' value='$code'>

 

My loop:

$codes=$_POST['codes'];

                      for($i=0;$i<count($codes);$i++)
                      {
                        if(isset($codes[$i]))
                        {
                          $status="$code was selected.";
                        }
                      else
                      {
                        $status="$code was not selected.";
                      }
                    }

 

I've been looking everywhere for an answer, but I can't seem to figure out where I'm going wrong. Help please?

 

Also, I thought it would be helpful to note that if I leave them all unselected, no message at all is shown (I don't currently have a default for $status). But even if I only check one, all of them receive the "was selected" message.

Link to comment
https://forums.phpfreaks.com/topic/205489-php-checkbox-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.