我试图通过我的邮件表单发送我的复选框值,但每次都返回“无”。在此先感谢您的帮助!
我的代码
<form method="post" name="sentMessage" id="contactForm" novalidate>
<div class="control-group form-group">
<div class="controls">
<label>Services Required:</label> <br />
<ul class="checkbox-grid">
<li><input type="checkbox" name="services[]" value="value1" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value2" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value3" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value4" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value5" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value6" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value7" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value8" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value9" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value10" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value11" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value12" /> Text 12</li>
</ul>
</div>
</div>
<button type="submit" class="btn btn-primary">Send Message</button>
</form>
我的PHP:
<?php
// check if fields passed are empty
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$type = $_POST['type'];
$other = $_POST['other'];
$date = $_POST['date'];
$venue = $_POST['venue'];
$budget = $_POST['budget'];
$message = $_POST['message'];
$selectedServices = 'None';
if(isset($_POST['services']) && is_array($_POST['services']) && count($_POST['services']) > 0){
$selectedServices = implode(', ', $_POST['services']);}
// create email body and send it
$to = 'mike@xxx.com'; // PUT YOUR EMAIL ADDRESS HERE
$email_subject = " Contact Form: $name"; // EDIT THE EMAIL SUBJECT LINE HERE
$email_body = "You have received a new message from your website's contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nType: $type\n\nOther: $other\n\nDate: $date\n\nVenue: $venue\n\nBudget: $budget\n\nServices: $selectedServices\n\nMessage:\n$message";
$headers = "From: noreply@xxx.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
有什么想法吗?我已经复制了这篇文章,但它似乎对我不起作用。 Adding checkboxes to PHP POST email form
编辑:修复语法错误
答案 0 :(得分:1)
您的语法错误似乎正在破坏表单。如果我运行包含在表单中的代码并在响应上运行PHP处理器,我将始终获得正确的结果。您的错误不在您提交的代码中。
我刚尝试并成功执行此操作(修复“email_body”语法问题后)。
为清晰起见而编辑
答案 1 :(得分:0)
可能是你错过了什么。运行代码。希望你能找到你的问题。
PHP代码:
if(isset($_POST['sub']) && $_POST['sub']=="Submit")
{
$selectedServices = 'None';
if(isset($_POST['services']) && is_array($_POST['services']) && count($_POST['services']) > 0)
{
$selectedServices = implode(', ', $_POST['services']);
}
echo $selectedServices;
}
<form method="post" action="">
<ul class="checkbox-grid">
<li><input type="checkbox" name="services[]" value="value1" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value2" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value3" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value4" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value5" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value6" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value7" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value8" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value9" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value10" /> Text 12</li>
<li><input type="checkbox" name="services[]" value="value11" /> Text 11</li>
<li><input type="checkbox" name="services[]" value="value12" /> Text 12</li>
</ul>
<input type="submit" id="sub" name="sub" value="Submit" />
</form>
输出:value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12