PHP 多复选框数组 [英] PHP Multiple Checkbox Array

查看:21
本文介绍了PHP 多复选框数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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'];
?>

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

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.

推荐答案

<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']); 
}
?>

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

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.

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

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

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

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

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

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

这篇关于PHP 多复选框数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆