0

I have a bunch of checkboxes each with a unique name and value using $_POST method. How can I put the selected values into an array? I started using a for loop, but I don't know how to call only one value at a time or determine whether it has been selected.

2 Answers 2

1

You want to do something like this:

<input type="checkbox" name="mycheckarray[]" value="1" />
<input type="checkbox" name="mycheckarray[]" value="2" />
<input type="checkbox" name="mycheckarray[]" value="3" />
<input type="checkbox" name="mycheckarray[]" value="4" />

Check boxes 2 and 4, then at the server side, if you print_r($_POST['mycheckarray']);, you will get something like this:

Array (
  [0] => 2
  [1] => 4
)
Sign up to request clarification or add additional context in comments.

Comments

0

You can create a group of checkboxes.

<form id="form1" name="form1" method="post" action="">
<label><input type="checkbox" name="CheckboxGroup[]" value="checkbox" id="CheckboxGroup1_0" />Checkbox 1</label>
<label><input type="checkbox" name="CheckboxGroup[]" value="checkbox" id="CheckboxGroup1_1" />Checkbox 2</label>
</form>

And after that u can use it as you like. print_r($_POST[CheckboxGroup]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.