1

I was under the impression that the server code (or at least PHP) that parses the POST of an HTML form cannot distinguish between a POST from a form with a checkbox whose value is not "checked" versus a POST from a form that doesn't include a checkbox by that name at all.

For example, take the following 3 forms. If they were all submitted as is, with no manual entry, just using the inital values, then forms f2 and f3 would send the same results:

<form name="f1">
<input type="text" name="txt_1" value="Hello">
<input type="checkbox" name="chk_1" checked="checked">
</form>

<form name="f2">
<input type="text" name="txt_1" value="Hello">
<input type="checkbox" name="chk_1">
</form>

<form name="f3">
<input type="text" name="txt_1" value="Hello">
</form>

In my real application, I'm submitting forms of the type f3, with a checkbox deliberately omitted (different checkboxes in different situations). For any checkbox I left missing, I wanted the back-end to just ignore it -- don't treat it as On or Off, just do nothing related to that field.

After I built it, I was almost going to throw out my work before testing, when I remembered that the back-end would treat a missing chk_1 exactly like it would an existing chk_1 that was unchecked -- and would turn Off the related value in the back-end.

But I went and tried it out, and IT WORKED! And I tried different variations, and they all work. They correctly disregard the fields related to missing checkboxes (while processing the fields related to existing checkboxes).

So, that's great, but I don't know how it's done (or whether it might stop working -- say on another browser, etc.). In PHP, I know that the code isset($_POST['chk_1']) will get the value of the checkbox, and it returns false in both cases: unchecked checkbox or missing checkbox. Is there a way in other server languages to distinguish? Why is this working?

oxfordian
  • 11
  • 2
  • 2
    http://www.w3.org/TR/html401/interact/forms.html#checkboxes – Musa Oct 17 '12 at 01:12
  • Thanks for the link, although all I can find in it is confirmation that my original understanding of it is correct: that only a CHECKED checkbox input is considered successful, and is therefore submitted with the POST. I'm still left mystified how this 3rd-party web service is able to detect that I omitted a checkbox entirely, and rather than treat that as an UNCHECKED checkbox, was able to ascertain that it was not submitted at all, and correctly ignored it. – oxfordian Oct 17 '12 at 03:13
  • Please specify some concrete evidence of a service that makes a distinction between a missing checkbox and an unchecked checkbox. – Jukka K. Korpela Oct 17 '12 at 06:09
  • 1
    Thanks for the contributions. It turns out -- false alarm. With more testing I found that the 3rd-party service was not clearing values even when an existing unchecked checkbox was sent. So the fact that it also did not clear the data values when checkboxes are not sent is no mystery. So -- never mind: the rule for forms in general is that sending a form with unchecked checkbox of a certain name is equivalent to sending a form with no checkbox at all by that name. – oxfordian Oct 17 '12 at 13:50

1 Answers1

1

The short answer is that PHP only gets what the Browser sends it. It doesn’t know or care about the form itself, just the data from the browser. The data comes as a collection of name=data values.

Among other factors, the browser will only send a button if

  • it has a name, and
  • it has been selected

If the button has no name, or if it was not selected, then PHP will never hear of it. This applies to

  • normal buttons (<button> or <input type="submit">)
  • radio buttons (where only one in a group can be selected: if none is selected, then this, too, will be absent)
  • checkboxes (only selected checkboxes are sent)

So, the short answer is that PHP will never know whether a checkbox is missing or simply unchecked, because neither will be sent to the server.

Manngo
  • 14,066
  • 10
  • 88
  • 110