Always Submit a Checkbox Value From HTML to PHP

Always Submit a Checkbox Value From HTML to PHP

I have a form, generated with PHP, presented in HTML that contains checkboxes. There are several reasons but, by design, if my checkboxes are ‘unticked’ they are not passed to my receiving script upon submission. No value is presented, and it is almost like the checkbox never existed. That’s irritating.

There are five approaches to work around this –

Use something else, a select for example or radio buttons

This isn’t an option for me – I have several fields in a restricted space, a checkbox is more or less my only solution. From a user interface position, I want the user to tick where an issue exists, but this is to be as unobtrusive as possible.

Ignore the fields

Unfortunately I need to know if an issue exists, but equally I need to know if no issue exists.

Consider the fields in advance

This would work – I know the fields I am expecting, I could just alter my PHP script to consider a checkbox ‘unticked’ if it isn’t present. However it’s not ideal – this form will be used to edit existing records, and from a code point of view I would like to loop through all $_REQUEST vars, build a query and set each key/field to it’s presented value. This will update the field if it has changed or leave it as was. I don’t want to have to break from this loop.

It’s also not ideal if the form changes – I want a submission script that will take what is thrown at it and send those fields on to the database as is. I don’t want to alter it every time I add a checkbox or other field.


You can place a hidden field behind each checkbox that ensures that the variable will always be sent, defaulting to ‘n’ the hidden element value. However if the checkbox is checked it will overtype the value to it’s own, ‘y’ like this -

<input type="hidden" name="check_box_1" value="n" />
<input type="checkbox" name="check_box_1" value="y" />

This is fine in practice, but hardly efficient or elegant – my form has a dynamic number of rows with 6 checkboxes each. That’s a lot of ghosting, and a pain to insert into the PHP script building the form.

Do something clever…

The only other option is to prepare the data before it is sent to the server. Using javascript I could turn all checkboxes to some other kind of element upon submission, perhaps setting hidden variables in a manner similar to ghosting (above).

My actual approach is to loop through all checkboxes on the page, changing the value and ‘ticking’ those that aren’t checked. Each checkbox starts with a value of ‘y’, however if it is not ‘ticked’ at submission, the script changes it’s value to ‘n’ and ‘ticks’ it. This means that the user will only ever see ticks where they have marked the box (or left it marked) but the receiving php script will always receive a variable and a positive confirmation – “this field exists and it’s value is ‘x’”.

function checkbox_setter(){
	if (!this.checked) {
		this.value = ''n'';
		this.checked = true;
  return true;

Calling the script is easy, either by assigning the below code to a button or by inserting it into an exisiting function.