How to send all checkboxes in POST? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 11:26:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!