PHP 多复选框数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14026361/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:27:20  来源:igfitidea点击:

PHP Multiple Checkbox Array

phparraysvariablescheckbox

提问by Darek Gajda

I've looked around for a few examples here but a lot of them are either too advanced for my grasp of PHP or their examples are too specific to their own projects. I am currently struggling with a very basic part of a PHP form.

我已经在这里查看了一些示例,但其中很多示例要么太高级,我无法掌握 PHP,要么它们的示例对于他们自己的项目来说过于具体。我目前正在努力处理 PHP 表单的一个非常基本的部分。

I am trying to create a form with a few checkboxes, each assigned a different value, I want these to be sent to a variable (array?) that I can echo/use later, in my case I will be sending the checked values in an email.

我正在尝试创建一个带有几个复选框的表单,每个复选框都分配了不同的值,我希望将它们发送到我可以稍后回显/使用的变量(数组?),在我的情况下,我将发送选中的值一封电邮。

So far, I have tried a few variations, but the closest I have come to it is this...

到目前为止,我已经尝试了一些变化,但我最接近的是这个......

<form method='post' id='userform' action='thisform.php'>
<tr>
    <td>Trouble Type</td>
    <td>
    <input type='checkbox' name='checkboxvar' value='Option One'>1<br>
    <input type='checkbox' name='checkboxvar' value='Option Two'>2<br>
    <input type='checkbox' name='checkboxvar' value='Option Three'>3
    </td>
</tr>
</table>
<input type='submit' class='buttons'>
</form>

<?php
$checkboxvar[] = $_REQUEST['checkboxvar'];
?>

Where I'd echo $checkboxvar[] into my email. Am I going about this completely wrong? The other idea I had was to use a lot of if statements.

我会在我的电子邮件中回显 $checkboxvar[] 的地方。我这样做是完全错误的吗?我的另一个想法是使用大量的 if 语句。

回答by cryptic ツ

<form method='post' id='userform' action='thisform.php'> <tr>
    <td>Trouble Type</td>
    <td>
    <input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
    <input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
    <input type='checkbox' name='checkboxvar[]' value='Option Three'>3
    </td> </tr> </table> <input type='submit' class='buttons'> </form>

<?php 
if (isset($_POST['checkboxvar'])) 
{
    print_r($_POST['checkboxvar']); 
}
?>

You pass the form name as an array and then you can access all checked boxes using the var itself which would then be an array.

您将表单名称作为数组传递,然后您可以使用 var 本身访问所有选中的框,这将是一个数组。

To echo checked options into your email you would then do this:

要将选中的选项回显到您的电子邮件中,您可以这样做:

echo implode(',', $_POST['checkboxvar']); // change the comma to whatever separator you want

Please keep in mind you should always sanitize your input as needed.

请记住,您应该始终根据需要清理您的输入。

For the record, official docs on this exist: http://php.net/manual/en/faq.html.php#faq.html.arrays

为了记录,官方文档存在:http: //php.net/manual/en/faq.html.php#faq.html.arrays

回答by Rawnay

add []in the name of the attributes in the input tag

[]在输入标签中添加属性的名称

 <form action="" name="frm" method="post">
<input type="checkbox" name="hobby[]" value="coding">  coding &nbsp
<input type="checkbox" name="hobby[]" value="database">  database &nbsp
<input type="checkbox" name="hobby[]" value="software engineer">  soft Engineering <br>
<input type="submit" name="submit" value="submit"> 
</form>

for PHP Code :

对于 PHP 代码:

<?php
 if(isset($_POST['submit']){
   $hobby = $_POST['hobby'];

   foreach ($hobby as $hobys=>$value) {
             echo "Hobby : ".$value."<br />";
        }
}
?>

回答by Martin Bean

You need to use the square brackets notation to have values sent as an array:

您需要使用方括号表示法将值作为数组发送:

<form method='post' id='userform' action='thisform.php'>
<tr>
    <td>Trouble Type</td>
    <td>
    <input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
    <input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
    <input type='checkbox' name='checkboxvar[]' value='Option Three'>3
    </td>
</tr>
</table>
<input type='submit' class='buttons'>
</form>

Please note though, that only the values of only checked checkboxes will be sent.

但请注意,只有选中的复选框的值才会被发送。

回答by Chaitanya Bhojne

Try this, by for Loop

试试这个,通过 for 循环

<form method="post">
<?php
for ($i=1; $i <5 ; $i++) 
{ 
    echo'<input type="checkbox" value="'.$i.'" name="checkbox[]"/>';
} 
?>
<input type="submit" name="submit" class="form-control" value="Submit">  
</form>

<?php 
if(isset($_POST['submit']))
{
    $check=implode(", ", $_POST['checkbox']);
    print_r($check);
}     
?>

回答by Udo E.

Also remember you can include custom indices to the array sent to the server like this

还请记住,您可以像这样在发送到服务器的数组中包含自定义索引

<form method='post' id='userform' action='thisform.php'>
<tr>
    <td>Trouble Type</td>
    <td>
    <input type='checkbox' name='checkboxvar[4]' value='Option One'>4<br>
    <input type='checkbox' name='checkboxvar[6]' value='Option Two'>6<br>
    <input type='checkbox' name='checkboxvar[9]' value='Option Three'>9
    </td>
</tr>
<input type='submit' class='buttons'>
</form>

This is particularly useful when you want to use the idof individual objects in a server array accounts(for instance)to send data back to the server and recognize same at server

当您想使用id服务器数组中的单个对象accounts(例如)将数据发送回服务器并在服务器上识别相同时,这特别有用

<form method='post' id='userform' action='thisform.php'>
<tr>
    <td>Trouble Type</td>
    <td>
    <?php foreach($accounts as $account) { ?>
        <input type='checkbox' name='accounts[<?php echo $account->id ?>]' value='<?php echo $account->name ?>'>
        <?php echo $account->name ?>
        <br>
    <?php } ?>
    </td>
</tr>
<input type='submit' class='buttons'>
</form>

<?php 
if (isset($_POST['accounts'])) 
{
    print_r($_POST['accounts']); 
}
?>

回答by Siddharth Shukla

if (isset($_POST['submit'])) {

for($i = 0; $i<= 3; $i++){

    if(isset($_POST['books'][$i]))

        $book .= ' '.$_POST['books'][$i];
}