php 提交带有空复选框的 HTML 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/476426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submit an HTML form with empty checkboxes
提问by Tray
I have an HTML form - with PHP, I am sending the data of the form into a MySQL database. Some of the answers to the questions on the form have checkboxes. Obviously, the user does not have to tick all checkboxes for one question. I also want to make the other questions (including radio groups) optional.
我有一个 HTML 表单 - 使用 PHP,我将表单的数据发送到 MySQL 数据库中。表格上的一些问题的答案有复选框。显然,用户不必勾选一个问题的所有复选框。我还想将其他问题(包括广播组)设为可选。
However, if I submit the form with empty boxes, radio-groups etc, I received a long list of 'Undefined index' error messages for each of them.
但是,如果我提交带有空框、无线电组等的表单,我会收到一长串“未定义索引”错误消息。
How can I get around this? Thanks.
我怎样才能解决这个问题?谢谢。
采纳答案by Gumbo
Unchecked radio or checkbox elements are not submitted as they are not considered as successful. So you have to check if they are sent using the issetor emptyfunction.
未选中的单选或复选框元素不会被提交,因为它们不被视为成功。所以你必须检查它们是否是使用issetorempty函数发送的。
if (isset($_POST['checkbox'])) {
// checkbox has been checked
}
回答by ceejayoz
I've used this technique from time to time:
我不时使用这种技术:
<input type="hidden" name="the_checkbox" value="0" />
<input type="checkbox" name="the_checkbox" value="1" />
note:This gets interpreted differentlyin different server-side languages, so test and adjust if necessary. Thanks to SimonSimCityfor the tip.
注:这被解释不同在不同的服务器端语言,所以测试,必要时调整。感谢SimonSimCity的提示。
回答by Greg
回答by idf
Here is a simple workaround using javascript:
这是使用 javascript 的简单解决方法:
before the form containing checkboxes is submitted, set the "off" ones to 0 and check them to make sure they submit. this works for checkbox arrays for example.
在提交包含复选框的表单之前,将“关闭”设置为 0 并检查它们以确保它们提交。例如,这适用于复选框数组。
///// example //////
///// 例子 //////
given a form with id="formId"
给定一个带有 id="formId" 的表单
<form id="formId" onSubmit="return formSubmit('formId');" method="POST" action="yourAction.php">
<!-- your checkboxes here . for example: -->
<input type="checkbox" name="cb[]" value="1" >R
<input type="checkbox" name="cb[]" value="1" >G
<input type="checkbox" name="cb[]" value="1" >B
</form>
<?php
if($_POST['cb'][$i] == 0) {
// empty
} elseif ($_POST['cb'][$i] == 1) {
// checked
} else {
// ????
}
?>
<script>
function formSubmit(formId){
var theForm = document.getElementById(formId); // get the form
var cb = theForm.getElementsByTagName('input'); // get the inputs
for(var i=0;i<cb.length;i++){
if(cb[i].type=='checkbox' && !cb[i].checked) // if this is an unchecked checkbox
{
cb[i].value = 0; // set the value to "off"
cb[i].checked = true; // make sure it submits
}
}
return true;
}
</script>
回答by user58670
To add to fmsf's code, when adding checkboxes I make them an array by having [] in the name
要添加到 fmsf 的代码中,在添加复选框时,我通过在名称中包含 [] 使它们成为一个数组
<FORM METHOD=POST ACTION="statistics.jsp?q=1&g=1">
<input type="radio" name="gerais_radio" value="primeiras">Primeiras Consultas por medico<br/>
<input type="radio" name="gerais_radio" value="salas">Consultas por Sala <br/>
<input type="radio" name="gerais_radio" value="assistencia">Pacientes por assistencia<br/>
<input type="checkbox" name="option[]" value="Option1">Option1<br/>
<input type="checkbox" name="option[]" value="Option2">Option2<br/>
<input type="checkbox" name="option[]" value="Option3">Option3<br/>
<input type="submit" value="Ver">
回答by tolginho
We are trouble on detecting which one checked or not.
我们很难检测哪一个被选中。
If you are populating form in a for loop, please use value property as a data holder:
如果您在 for 循环中填充表单,请使用 value 属性作为数据持有者:
<?php for($i=1;$i<6;$i++):?>
<input type="checkbox" name="active[]" value="<?php echo $i ?>"
<?endfor;?>
If submit form you'll get order numbers of checkboxes that checked (in this case I checked 3rd and 4th checkboxes):
如果提交表单,您将获得选中的复选框的订单号(在这种情况下,我选中了第 3 个和第 4 个复选框):
array(1) {
["active"]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "4"
}
}
When you are processing form data in loop, let's say in post.php, use following code to detect if related row is selected:
当您循环处理表单数据时,假设在 post.php 中,使用以下代码检测是否选择了相关行:
if(in_array($_POST['active'] ,$i))
$answer_result = true;
else
$answer_result = false;
Final code for testing:
最终测试代码:
<?php if (isset($_POST) && !empty($_POST)):
echo '<pre>';
var_dump($_POST);
echo '</pre>';
endif;
?>
<form action="test.php" method="post">
<?php for($i=1;$i<6;$i++):?>
<input type="checkbox" name="active[]" value="<?php echo $i; ?>" />
<?php endfor;?>
<button type="submit">Submit</button>
</form>
回答by Cruachan
Use this
用这个
$myvalue = (isset($_POST['checkbox']) ? $_POST['checkbox'] : 0;
Or substituting whatever your no value is for the 0
或者用你的无价值代替 0
回答by Damir Olejar
Although many answers were submitted, I had to improvise for my own solution because I used the customized check-boxes. In other words, none of the answers worked for me.
尽管提交了许多答案,但我不得不为自己的解决方案即兴发挥,因为我使用了自定义的复选框。换句话说,没有一个答案对我有用。
What I wanted to get is an array of check-boxes, with on and off values. The trick was to submit for each check-box on/off value a separator. Lets say that the separator is ";" so the string you get is
我想要的是一组复选框,带有打开和关闭值。诀窍是为每个复选框的开/关值提交一个分隔符。让我们说分隔符是“;” 所以你得到的字符串是
;, on, ;, ;, ;
;, 在, ;, ;, ;
Then, once you get your post, simply split the data into array using the "," as a character for splitting, and then if the array element contains "on", the check-box is on, otherwise, it is off.
然后,一旦你得到你的帖子,只需使用“,”作为拆分字符将数据拆分为数组,然后如果数组元素包含“on”,则复选框为on,否则为off。
For each check-box, change the ID, everything else is the same... and syntax that repeats is:
对于每个复选框,更改 ID,其他一切都相同……重复的语法是:
<div>
<input type="hidden" name="onoffswitch" class="onoffswitch-checkbox" value=";" />
...some other custom code here...
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch1" checked>
</div>
EDIT: instead of the ";", you can use some KEY string value, and that way you will know that you did not mess up the order, once the POST is obtained on the server-side... that way you can easily create a Map, Hash, or whatever. PS: keep them both within the same div tag.
编辑:而不是“;”,你可以使用一些KEY字符串值,这样你就会知道你没有弄乱顺序,一旦在服务器端获得POST......这样你就可以轻松创建 Map、Hash 或其他任何东西。PS:将它们都放在同一个 div 标签中。

