0

I'm generating a form with php/mysql. I'm using checkbox that looks like that:

<input type="checkbox" id="my2_3" name="my2_3" />
<input type="checkbox" id="my2_4" name="my2_4" />
<input type="checkbox" id="my2_5" name="my2_5" />

My issue is to retrieve those data (whether the checkbox is checked or not and the id). How can I retrieve that with php without knowing in advance what will be the $_POST[""] to request?

0

4 Answers 4

1
<input type="checkbox" id="my2_3" name="my[]" value="my2_3" />
<input type="checkbox" id="my2_4" name="my[]" value="my2_4" />
<input type="checkbox" id="my2_5" name="my[]" value="my2_5" />

I changed the name attribute to an array,

foreach($_POST['check'] as $value) {
   $check_msg .= "Checked: $value\n";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use foreach($_POST as $key => $value) { ... } to iterate over all POST vars.

1 Comment

Thanx a lot! If i understood it right I would have some thing like that for <input type="checkbox" id="my2_3" name="my2_3" /> $key = my2_3 and $value = false
0

It'd be easier for you if you changed the name attribute to create a php array. Check the documentation on creating arrays in HTML form.

Comments

0

Checkboxes are only posted when they are ticked. So you need to inspect $_POST and use isset() to determine whether or not the key you are looking for (the name attribute of a checkbox) is present. If it is, the checkbox was ticked. If not, the checkbox was unticked.

Comments

Your Answer

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