php - PHP 表单上的多个复选框选项

标签 php html css forms

已经使用 PHP 构建了一个功能表单,但是我有一个包含 5 个选项的复选框(可以选择超过 1 个),但是电子邮件中只显示了 1 个选项。

HTML:

<label class="radio"><input type="checkbox" name="service" value="Web Design" />Web Design</label>
<label class="radio"><input type="checkbox" name="service" value="Graphic Design" />Graphic Design</label>
<label class="radio"><input type="checkbox" name="service" value="Brand Identity" />Brand Identity</label>
<label class="radio"><input type="checkbox" name="service" value="Online Marketing" />Online Marketing</label>
<label class="radio"><input type="checkbox" name="service" value="Other" />Other</label>

PHP: ($service)

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$service = $_POST['service'];
$description = $_POST['description'];
$inspiration = $_POST['inspiration'];
$budget = $_POST['budget'];
$deadline = $_POST['deadline'];
$formcontent=" From: $name \n Email: $email \n Phone: $phone \n Website: $website \n\n service: $service \n budget: $budget \n deadline: $deadline \n\n description: $description \n\n inspiration: $inspiration ";
$recipient = "myemail@gmail.com";
$subject = "Contact form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You, your information has been received.";
?>

最佳答案

如果您有多个同名元素,那么您将只能读取其中一个,除非您使用数组语法 [] 例如name=service[] 那么您将收到 $_POST['service'] 作为包含您所有服务的数组。您可以使用 implode将其转换为字符串。

$service = implode(' ', $_POST['service']);

关于php - PHP 表单上的多个复选框选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12907283/

相关文章:

php - SMTP 错误 : Failed to connect to server: Connection timed out (110) with PHPMailer and Outlook SMTP

javascript - 用什么来代替 $(document).ready();?

html - Bootstrap radio 组 - 水平对齐

javascript - 我有一个无序列表,我想滚动到可点击的元素

javascript - LTR 到 RTL jQuery Collapsibles block

php - 即时创建数据库备份

php - 如何使用 php mailer 将数据库数据作为电子邮件发送?

php - 有没有人为 codeigniter 和 uploadify session 问题提供新的解决方案

java - 如何使用 OutputStreamWriter 在同一浏览器窗口中写入流?

html - 尝试设置单选按钮的样式(使用背景图像)