PHP多阵列复选框 [英] PHP Multiple Checkbox Array

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

问题描述

我环顾四周,这里的几个例子,但他们中的很多要么是我的PHP或他们的榜样的把握太超前太具体到自己的项目。我目前正在与一个PHP形式的一个非常基本的部分努力。

我试图创建一些复选框形式,每个分配一个不同的价值,我希望这些被发送到一个变量(数组?),我可以附和/使用后,在我的情况下,我会送在一封电子邮件中检查值。

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

 <形式方法='后'ID ='用户窗体'行动='thisform.php'>
&所述; TR>
    < TD>故障类型< / TD>
    &所述; TD>
    <输入类型=复选框名称='checkboxvar'值='方案一'> 1 LT; BR>
    <输入类型=复选框名称='checkboxvar'值='方案二'> 2'; BR>
    <输入类型=复选框名称='checkboxvar'值=方案三→3
    < / TD>
< / TR>
< /表>
<输入类型=提交类='按钮'>
< /表及GT;< PHP
$ checkboxvar [] = $ _REQUEST ['checkboxvar'];
?>

在哪里我回声$ checkboxvar []进入我的电子邮件。
我要对这个完全错误的?其他的想法,我必须是使用大量的if语句。


解决方案

 <形式方法='后'ID ='用户窗体'行动='thisform.php'> &所述; TR>
    < TD>故障类型< / TD>
    &所述; TD>
    <输入类型=复选框名称='checkboxvar []'值='方案一'> 1 LT; BR>
    <输入类型=复选框名称='checkboxvar []'值='方案二'> 2'; BR>
    <输入类型=复选框名称='checkboxvar []'值=方案三→3
    < / TD> < / TR> < /表> <输入类型=提交类='按钮'> < /表及GT;< PHP
如果(使用isset($ _ POST ['checkboxvar']))
{
    的print_r($ _ POST ['checkboxvar']);
}
?>

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

要呼应勾选的选项到您的电子邮件,那么你会做这样的:

 回声破灭('',$ _ POST ['checkboxvar']); //改变逗号任何你想要的分隔符

请记住,你应该总是净化你的输入需要。

有关记录,在此存在官方的文档:<一href=\"http://php.net/manual/en/faq.html.php#faq.html.arrays\">http://php.net/manual/en/faq.html.php#faq.html.arrays

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

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

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

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

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

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