I am trying to add a disabled checkbox using Zend_Form and then enable it with JS.
Here’s the problem, zend adds a hidden input field with the same name and the value 0. This is to make sure even if the checkbox is not checked a value of 0 will be sent rather than no value at all. This is great. But if you create your checkbox as disabled, zend does not output this hidden field. All it outputs is the checkbox, disabled, being checked or not (does not matter since it is disabled it wont submit anything at all).
I need to allow the client to enable the checkbox and change the value.
If it is disabled, nothing is submitted, the db checkbox value stays the same. If it is enabled the client should be able to check/uncheck it. The problem is now there is no hidden field, and if the checkbox is unchecked nothing is submitted.
Any ideas?
First: I don’t like the way Zend handled this for you. Adding the hidden field with value 0 simply isn’t the same as a disabled checkbox, as you found out yourself.
To solve your problem: Simply add a hidden field by yourself with the original state in it.
eg:
Now you can check the value of your checkbox by first checking the value of $_POST[“originalStateOfMyCheckbox”].
If enabled: you can use isset($_POST[“yourcheckboxname”]) to find out if it was checked.
If disabled: you can use again isset($_POST[“yourcheckboxname”]) to find out if your javascript enabled it.