Tag-Archive for » checkbox «

Thursday, March 12th, 2009 | Author: viking

The HTML specification dictates that unchecked check boxes should not supply a value in a form submission, which can be problematic for applications wishing to set a flag in a database based on an unchecked check box. Rails gets around this by using a trick in the check_box helper. It places a hidden input tag with the requested default value directly after the check box tag.

The reason this works is because form parameters are sent in the order that they appear. This means the hidden input value is submitted after the check box value would be, and Rails gives precedence to parameters appearing earlier in the request.

I tried this in PHP, hoping it would behave the same way. It doesn’t. It turns out that PHP overwrites earlier variables in the request with later ones. This behavior is dictated by the request_order directive (as of PHP 5.3.0, which is still in beta at the time of this post). The fix is simply to place the hidden input tag before the check box tag.

Hopefully this helps someone avoid the moderate pain I went through figuring all this out.

Category: php, rails  | Tags: ,  | Leave a Comment