我有一个包含多个复选框的联系表单。我已将 [] 添加到名称中,但不确定在 PHP 中要做什么。如果没有 [],我只能在发送的电子邮件中显示第一个复选框项目。添加[]后,我什么也没得到。
(注:我曾尝试在 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();
}
扬帆大鱼
守候你守候我