在此方面提供一些帮助将不胜感激:
我有一组复选框,鼓励用户至少选择一个选项。我想使用 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/