Check out Scrivo

Do you want to try out Scrivo? Then here's a demo for you that does not just get your feet wet but lets you plunge right in.

Contact us

For more information, please contact us. We're happy to help you out!

Next Oct 6 Previous

Blank checks

Take this elementary HTML form:

<form method="POST">
    <input type="checkbox" name="coffee">
    <button type="submit">Save</button>
</form>

What will be the value of $_POST["coffee"] when the form is submitted and the check box is not set.

A: false
B:
C: the entry "coffee" will not be set in the $_POST array
D: the form is not submitted because no action is given.

Answer

First of all the form will be submitted without any problems: the form's action will default to the sending URL. So answer D is false.

To answer this question it is best to look at the data send by this form. The request data send will something look like:

POST /zend_questions/test.html HTTP/1.1
Host: heiligehostie
Connection: keep-alive
Content-Length: 0
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin: http://heiligehostie
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit (KHTML) Ubuntu 98765
Content-Type: application/x-www-form-urlencoded
Referer: http://heiligehostie/zend_questions/test.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: atuvc=0%5C22%2C0%7C23; PHPSESSID=asdfaaaiv1234rdf

[/code]

When your look at the data posted you'll see that there's no checkbox data set. The consequence is that this value is also not set in the $_POST array. Although it's very annoying that you'll need isset() to test it, PHP can't be blamed for that: it has no way of knowing it.