Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit an HTML form with empty checkboxes

I have an HTML form - with PHP, I am sending the data of the form into a MySQL database. Some of the answers to the questions on the form have checkboxes. Obviously, the user does not have to tick all checkboxes for one question. I also want to make the other questions (including radio groups) optional.

However, if I submit the form with empty boxes, radio-groups etc, I received a long list of 'Undefined index' error messages for each of them.

How can I get around this? Thanks.

like image 861
Tray Avatar asked Jan 24 '09 18:01

Tray


People also ask

How do you make a checkbox unchecked in HTML?

attr("checked","checked"); To uncheck the checkbox: $("#checkboxid").

How submit form if checkbox is checked?

If you need to submit a form when a checkbox is checked or when it is unchecked like when you are using a switch, a good way is to create an hidden input. If you try to submit the checkbox argument if the checkbox is unchecked the form will not be submitted at all.

How do I make a checkbox in HTML form?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!


2 Answers

I've used this technique from time to time:

<input type="hidden" name="the_checkbox" value="0" /> <input type="checkbox" name="the_checkbox" value="1" /> 

note: This gets interpreted differently in different server-side languages, so test and adjust if necessary. Thanks to SimonSimCity for the tip.

like image 164
ceejayoz Avatar answered Sep 17 '22 13:09

ceejayoz


Unchecked radio or checkbox elements are not submitted as they are not considered as successful. So you have to check if they are sent using the isset or empty function.

if (isset($_POST['checkbox'])) {     // checkbox has been checked } 
like image 31
Gumbo Avatar answered Sep 19 '22 13:09

Gumbo