1

已经用 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: ($服务)

<?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.";
?>
4

4 回答 4

1

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

$service = implode(' ', $_POST['service']);
于 2012-10-16T03:58:50.853 回答
1

在名称中使用数组,您将其命名为服务写入那里,服务 []。然后使用 multiple="multiple" tag.and 在 php 部分使用这个:

<?php
$services=implode(',',mysql_real_escape_string($_post['service']));
print_r($services);
?>

快乐编码!

于 2012-10-16T08:26:24.247 回答
0

name="service[]"(Array)name="service"放在您的表单中,以获取多个选定的值作为数组。在检索时,您可以使用implode以下函数获取值

$services = implode(",", $_POST['service']);
于 2012-10-16T03:53:30.667 回答
0

用户数组名称而不是复选框中的简单名称,如下所示:

<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>

print_r($_POST['service']);
// output
Array
(
    [0] => Web Design
    [1] => Graphic Design
    [2] => Brand Identity
    [3] => Online Marketing
    [4] => Other
)
于 2012-10-16T03:55:29.727 回答