PHP表单上的多个复选框选项 [英] Multiple checkbox options on a PHP form

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

问题描述

已经使用PHP构建了一个函数形式,但是我有一个带有5个选项的复选框(可以选择多个选项),但是只有一个选项出现在电子邮件中。

have built a functional form with PHP however I have a checkbox with 5 options (more than 1 can be selected) however only 1 option is showing up in the email.

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 将其转换为字符串。

If you have multiple elements with the same name then you will only be able to read one of them unless you use the array syntax [] e.g. name=service[] then you will receive $_POST['service'] as an array containing all your services. You can use implode to convert it to a string.

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

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

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