php 从 POST 获取复选框数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10655355/
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
getting a checkbox array value from POST
提问by arboles
i am posting an array of checkboxes. and i cant get it to work. i didnt include the proper syntax in the foreach loop to keep it simple. but it is working. i tested in by trying to do the same thing with a text field instead of a checkbox and it worked with the textfield.
我正在发布一系列复选框。我无法让它工作。我没有在 foreach 循环中包含正确的语法以保持简单。但它正在工作。我通过尝试使用文本字段而不是复选框来执行相同的操作来进行测试,并且它与文本字段一起使用。
<form method="post">
<?php
foreach{
echo'
<input id="'.$userid.'" value="'.$userid.'" name="invite[]" type="checkbox">
<input type="submit">';
}
?>
</form>
here is the part that is not working. it is echoing 'invite' instead of array.
这是不起作用的部分。它正在呼应“邀请”而不是数组。
<?php
if(isset($_POST['invite'])){
$invite = $_POST['invite'];
echo $invite;
}
回答by Sean
Your $_POST array contains the invite array, so reading it out as
您的 $_POST 数组包含邀请数组,因此将其读出为
<?php
if(isset($_POST['invite'])){
$invite = $_POST['invite'];
echo $invite;
}
?>
won't work since it's an array. You have to loop through the array to get all of the values.
不会工作,因为它是一个数组。您必须遍历数组以获取所有值。
<?php
if(isset($_POST['invite'])){
if (is_array($_POST['invite'])) {
foreach($_POST['invite'] as $value){
echo $value;
}
} else {
$value = $_POST['invite'];
echo $value;
}
}
?>
回答by Tom Pietrosanti
I just used the following code:
我只是使用了以下代码:
<form method="post">
<input id="user1" value="user1" name="invite[]" type="checkbox">
<input id="user2" value="user2" name="invite[]" type="checkbox">
<input type="submit">
</form>
<?php
if(isset($_POST['invite'])){
$invite = $_POST['invite'];
print_r($invite);
}
?>
When I checked both boxes, the output was:
当我选中两个框时,输出是:
Array ( [0] => user1 [1] => user2 )
I know this doesn't directly answer your question, but it gives you a working example to reference and hopefully helps you solve the problem.
我知道这并不能直接回答您的问题,但它为您提供了一个可供参考的工作示例,并希望能帮助您解决问题。
回答by Ecropolis
Check out the implode() function as an alternative. This will convert the array into a list. The first param is how you want the items separated. Here I have used a comma with a space after it.
查看 implode() 函数作为替代方法。这会将数组转换为列表。第一个参数是您希望如何分隔项目。在这里,我使用了一个逗号,后面有一个空格。
$invite = implode(', ', $_POST['invite']);
echo $invite;
回答by djot
// if you do the input like this
<input id="'.$userid.'" value="'.$userid.'" name="invite['.$userid.']" type="checkbox">
// you can access the value directly like this:
$invite = $_POST['invite'][$userid];
回答by Ryan
Because your <form>element is inside the foreach loop, you are generating multiple forms. I assume you want multiple checkboxes in one form.
因为您的<form>元素在 foreach 循环内,所以您正在生成多个表单。我假设您想要一种形式的多个复选框。
Try this...
尝试这个...
<form method="post">
foreach{
<?php echo'
<input id="'.$userid.'" value="'.$userid.'" name="invite[]" type="checkbox">
<input type="submit">';
?>
}
</form>

