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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.