0

I have a form, and inside it there's a while() with checkboxes.

I wish to have so when you mark and then press submit, the message will remove.

Now, I have the checkboxes, and the submit button and so. Now all my checkboxes are like this:

   <input class="cbPick" name="cbPick" type="checkbox" value="<?php echo $id; ?>">

How can i work with that in PHP? Should i do, $_POST["cbPick"] to know if its marked or not ?

And when i have more with these, how can i know which is which?

3 Answers 3

4

Check boxes require the use of an array. PHP will automatically place the checked boxes into an array if you place [] brackets at the end of each name.

Please choose type of food:<br />
Steak:<input type="checkbox" value="Steak" name="food[]">:<br />
Pizza:<input type="checkbox" value="Pizza" name="food[]">:<br />
Chicken:<input type="checkbox" value="Chicken" name="food[]">:<br />

Then you can do

$foodArray=$_POST['food'];
echo $foodArray[0]; //Steak Value
echo $foodArray[1]; //Pizza Value
echo $foodArray[2]; //Chicken Value

PS - This information was found by Googling "Checkboxes PHP" and clicking the first link. I encourage you to do at least a little research in the future before posting a question that has anwers so readily available.

Sign up to request clarification or add additional context in comments.

2 Comments

That is assuming the checkboxes are being used for selecting things from a list. In some cases however, such as an "I Agree" checkbox for a terms & conditions page, this is not needed.
The OP stated that there are checkboxes create by a loop. I think it goes without saying this is not such a case.
0
if (!empty($_POST['cbPick']])) {
  // Do stuff here
}

empty() checks first for existence, then if the value is non-nullish (null, zero, empty string, etc). That way you won't get a notice if the array key doesn't exist. Checkboxes are a little weird in that they only come through in the $_POST array if they are checked. Otherwise the variable is not posted at all.

1 Comment

Ok, and when i have something in the value="" how can i ouput that?
0

If you use the same name (cbPick[]) for a few checkboxes, $_POST["cbPick"] will be filled as an array with each of the checked values.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.