php - 使用 PHP 检索多个复选框值

标签 php html post

在此方面提供一些帮助将不胜感激:

我有一组复选框,鼓励用户至少选择一个选项。我想使用 PHP 将所有这些选项存储在一个变量中,然后依次将此变量附加到电子邮件正文的末尾,以使用电子邮件发送给站点主机。但是,我似乎只能检索用户选择的最后一个选项。我尝试过使用数组语法(例如 name="instrument[]"),但我似乎无法找到从该数组中检索值的方法。我已使用“内爆”方法存储发布值,但我不断收到一条错误消息,显示“警告:内爆():C:\xampp\htdocs\academy\contact-process.php 中传递的参数无效8..

我的代码如下:

联系人.html

<tr>
    <td>
        <label id="checkbox">Piano</label>
        <input id="piano" type="checkbox" name="instrument[]" value="Piano" class="required" title="Please check at least one option.">
    </td>
    <td>
        <label id="checkbox">Flute</label>
        <input id="flute" type="checkbox" name="instrument[]" value="Flute">
    </td>
    <td>
        <label id="checkbox">Singing</label>
        <input id="singing" type="checkbox" name="instrument[]" value="Singing">
    </td>
</tr>


<tr>
    <td>
        <label id="checkbox">Violin</label>
        <input id="violin" type="checkbox" name="instrument[]" value="Violin">
    </td>

联系过程.php:

<pre><?php


$title = $_POST["title"];
$forname = $_POST["firstname"];
$surname = $_POST["lastname"];
$phone = $_POST["phone"];
$instrument = implode(' ', $_POST["instrument"]);
$hear = $_POST["method"];       
$enrole = $_POST["child"];
$dob = $_POST["date"];
$message = $_POST["enquiry"];




$email_body = "";
$email_body = $email_body . "Title: " . $title . "\n";
$email_body = $email_body . "Forname: " . $forname . "\n";
$email_body = $email_body . "Surname: " . $surname . "\n";
$email_body = $email_body . "Telephone Number: " . $phone . "\n";
$email_body = $email_body . "Heard About You From: " . $hear . "\n";
$email_body = $email_body . "Interested In Learning: " . $instrument . "\n";
$email_body = $email_body . "Would Like To Enrole A(n): " . $enrole . "\n";
$email_body = $email_body . "Child's Date of Birth: " . $dob . "\n";
$email_body = $email_body . "Message: " . $message;

echo $email_body; 

?></pre>

目前这会生成如上所述的错误消息。

有人知道解决方案吗?

非常感谢您的回复!!

罗伯特。

最佳答案

现在下面是您的数组输入类型。

 <input id="singing" type="checkbox" name="instrument[]" value="Singing">

你必须像这样循环处理它

 foreach($_POST['instrument'] as $instrument)
{
    echo $instrument.'<br>';
}

关于php - 使用 PHP 检索多个复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17274632/

相关文章:

php - 在 phpexcel 中将图像添加到 Excel

php - MAC 操作系统上的 Cron 作业问题

javascript - datepicker 更改月份未在 Chrome 中打开

php - Laravel 5.5 - 查询多个条件,但仅当 POST 数据等于 1 时

php - 使用 php 动态查找 sql 列一个月的总值

html - 未应用文档头中的 css 样式

javascript - 我想将图像放置在 html 表格中

post - 在带有 SuiteScript 2.0 的 NetSuite 中,无法使用内容类型为 multipart/form-data 的 HTTP POST 请求发送文件

javascript - Firebase REST API - 如何设置字符编码?

html - 将表单提交到没有地址栏的新窗口