【发布时间】:2020-12-09 04:16:28
【问题描述】:
我有一个包含多个复选框的联系表单。我已将 [] 添加到名称中,但不确定在 PHP 中要做什么。如果没有 [],我只会在发送的电子邮件中显示第一个复选框项目。添加[]后,我得到了nada。
(注意:我曾尝试在 StackOverflow 中使用类似的问题,但它们并没有解决我的困境。)
这是我的代码(我已将 HTML 截断为仅显示复选框区域):
<div class="form-check">
<input type="checkbox" class="form-check-input" name="findout[]" id="checkbox5" value="Advertisement">
<label for="findout[]" class="form-check-label">Advertisement</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="findout[]" id="checkbox6" value="Yard Sign">
<label for="findout[]" class="form-check-label">Yard Sign</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="findout[]" id="checkbox7" value="Search Engine">
<label for="findout[]" class="form-check-label">Search Engine</label>
</div>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$findout = $_POST['findout'];
$method = $_POST['method'];
$message = $_POST['message'];
$subject = $_POST['subject'];
header('Content-Type: application/json');
if ($name === ''){
print json_encode(array('message' => 'Name cannot be empty', 'code' => 0));
exit();
}
if ($email === ''){
print json_encode(array('message' => 'Email cannot be empty', 'code' => 0));
exit();
} else {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
print json_encode(array('message' => 'Email format invalid.', 'code' => 0));
exit();
}
}
if ($subject === ''){
print json_encode(array('message' => 'Subject cannot be empty', 'code' => 0));
exit();
}
if ($message === ''){
print json_encode(array('message' => 'Message cannot be empty', 'code' => 0));
exit();
}
$content="From: $name \nEmail: $email \nHow did you find out about us?: $findout \nPreferred way of contact: $method \nMessage: $message";
$recipient = "emailaddress@domain.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
print json_encode(array('message' => 'Email successfully sent!', 'code' => 1));
exit();
?>
感谢任何帮助。
马哈罗! 克里斯
【问题讨论】:
标签: php html contact-form