[英]How to send multiple checkbox responses from HTML form via php?
I have set up a contact form, and set it to email the response to an email account. 我已经建立了联系表格,并将其设置为通过电子邮件将回复发送到电子邮件帐户。 Part of the form is a series of checkboxes, and I need to get these to display in the email as a list.
表单的一部分是一系列复选框,我需要将其作为列表显示在电子邮件中。 This is the code I have below, which at the moment returns 'Array' instead of the values of the checkboxes.
这是我下面的代码,此刻现在返回“ Array”而不是复选框的值。 Any suggestions?
有什么建议么?
HTML: HTML:
<h3>Service required:</h3>
<input type="text" id="name" name="name" placeholder="Name" required />
<input type="email" id="email" name="email" placeholder="Email" required />
<input class="check-box styled" type="checkbox" name="service[]" value="Service / repairs" /><label> Service / repairs</label>
<input class="check-box styled" type="checkbox" name="service[]" value="MOT" /><label> MOT</label>
<input class="check-box styled" type="checkbox" name="service[]" value="Cars for sale" /><label> Cars for sale</label>
Here's the php: 这是PHP:
<?php
if (isset($_POST['service'])) {
$service = $_POST['service'];
// $service is an array of selected values
}
$formcontent= "From: $name \n Service(s) required: $service \n";
$recipient = "name@email.com";
$subject = "You have a new message from $name";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will get back to you as soon as we can.";
?>
Thanks, 谢谢,
Jason 杰森
You should join (for example implode with ', ') your array elements to a string. 您应该将数组元素连接(例如,以','内爆)到字符串。
<?php
$formcontent= "From: $name \n Service(s) required: ".implode(", " ,$service)." \n";
?>
Why don't you loop through the array to get the desired results into a String? 为什么不遍历数组以将所需的结果转换为String?
if (isset($_POST['service'])) {
$service = $_POST['service'];
// $service is an array of selected values
$service_string = "";
for($i=0;$i<count($service);$i++)
{
if($i!=0)
{
$service_string = $service_string . ", ";
}
$service_string = $service_string . $service[$i];
}
}
You will then get an output of a comma separated list of each ticked item as $service_string. 然后,您将获得每个选中项目的逗号分隔列表的输出,作为$ service_string。
Since several checkboxes are stored in $_POST['service']
, it is an array itself and has become two-dimensional. 由于
$_POST['service']
中存储了多个复选框,因此它本身就是一个数组,并且已经变成二维的。 Its different indexes are accessible like this: $_POST['service'][0]
. 它的不同索引可以这样访问:
$_POST['service'][0]
。
To do something with $_POST['service']
, you can use foreach to loop through all indexes: 要使用
$_POST['service']
,可以使用foreach遍历所有索引:
foreach($_POST['service'] as $post){
//Do stuff here
}
Alternatively, use implode() to simply concatenate all indexes. 或者,使用implode()来简单地连接所有索引。
Your input type checkbix must have unique names. 您的输入类型checkbix必须具有唯一的名称。 Otherwise last checkbox will be found in $_POST.
否则,将在$ _POST中找到最后一个复选框。 Or you can loop through as discuss above.
或者,您可以按照上面的讨论进行遍历。 Make your email html format and write a string of html to $formcontent.
将您的电子邮件设置为html格式,并将html字符串写入$ formcontent。 eg
例如
$formcontent = "<html><head></head><body>";
$formcontent .= "<ul><li>".$_POST["checkbox1"]."</li>";
$formcontent .= "<li>".$_POST["checkbox2"]."</li>";
$formcontent .= "</ul></body></html>";
To write email in html format see mail function on php website. 要以html格式编写电子邮件,请参阅php网站上的mail功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.