问题
I have 10 checkboxes in a form. Only checked checkboxes are sending to POST. How to send all checkboxes to POST, beacouse I want to do instruction for changed to checked and changed to no checked checkbox. How to do it?
回答1:
Only checked checkboxes are sent. A common strategy is to set hidden inputs with the same name and the default unchecked value. Just make sure the checkbox input comes after the hidden input:
<input type="hidden" name="cb" value="0">
<input type="checkbox" name="cb" value="1">
Then there will always be a $_POST['cb'] with value 0 for not checked and value 1 for checked.
回答2:
As I already stated in a comment, the browser will only send the checked checkboxes and will never send all checkboxes. What you can do is to check with PHP if the checkbox is present in the $_POST or $_GET variable and act according to the present state.
If you dynamically generate the checkboxes with a PHP script, you can make a hidden box with a serialized array with all the checkbox names to check and then you can only loop through it and check their states later on in the validation script.
Since you didn't provide any code I can't help you with it or make any improvements.
来源:https://stackoverflow.com/questions/31925294/how-to-send-all-checkboxes-in-post