[编辑 - 添加了一个完整的表单摘要]在我的HTML中 - 我有一个复选框 - 看起来像这样:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet" />
<form class="contact-form row" id="feedbacks" method="POST" action="feedback.php">
<div class="col-xs-10 col-xs-offset-1">
<fieldset class="form-group">
<label for="full_name"></label>
<input type="text" class="form-control" placeholder="Your name and surname" name="full_name" id="full_name">
</fieldset>
<fieldset class="">
<label for="feedback_email"></label>
<input type="text" class="form-control" placeholder="Your Email address" name="feedback_email" id="feedback_email">
</fieldset>
<div class="checkbox">
<label class="c-input c-checkbox">
<input type="checkbox" name="subbed" id="subbed">
<span class="c-indicator"></span>
<span class="text-muted m-l-1">subscribe to <abbr class="msa"></abbr> notifcation service.</span>
</label>
</div>
<fieldset>
<label for="message"></label>
<textarea class="form-control" rows="9" placeholder="Your message here.." name="message" id="message"></textarea>
</fieldset>
<div class="row m-t-1">
<fieldset class="col-xs-4 col-xs-offset-4">
<button class="btn btn-primary btn-block btn-lg" name="submit" type="submit" id="send_feedback">Send <i class="ion-android-arrow-forward"></i>
</button>
</fieldset>
</div>
</div>
</form>
<input type="checkbox" name="subscribed" id="subscribed" value="sub_me">
在我的PHP中,我创建了一个变量$subscribe
,它链接到subscribed
复选框。
当复选框单独存在时,PHP应该发送"I would not like to recieve news emails"
的电子邮件。为实现这一点,我选择在PHP表单验证代码中使用以下三元组:
[编辑 - 提供所有PHP]
<?php
$value = '';
$error = "";
$error_message = "";
$info = "";
if($_SERVER['REQUEST_METHOD'] == "POST"){
$subscribe = 'Would ' . (isset($_POST['subbed']) && $_POST['subscribed'] == 'sub_me' ? 'like to ' : 'not like to ') . 'receive news emails.';
$admin_email = "myemail_address@gmail.com";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <noreply@domain.ac.za>' . "\r\n";
$headers .= 'Reply-To: ' . $feedback_email . "\r\n";
'X-Mailer: PHP/' . phpversion();
$full_name = $_POST['full_name'];
$feedback_email = $_POST['feedback_email'];
$feedback = $_POST['message'];
$rep_message = "some thank you message to " . $full_name;
$message = 'another message which references ' . $subscribe;
$reply = 'some message consisting of ' . $full_name;
mail($admin_email,"Feedback",$message,$headers,"-fforwardguy@gmail.com");
mail($feedback_email,"Feedback",$reply,$headers);
}
?>
[编辑]问题是表单只被确认为已发送,但没有收到任何数据。
[编辑额外信息后 - 以下事项是否会影响表单提交? `
AJAX
提交的。(AJAX
函数有效,运行成功函数。)
答案 0 :(得分:0)
好的 - 所以在我注意到实际问题是什么之后 - 我发现我的AJAX
函数正在从错误的表单中收集数据。
答案 1 :(得分:-1)
试试这个:
$subscribe = 'Would ' . (isset($_POST['subscribed']) && $_POST['subscribed'] == 'sub_me' ? 'like to ' : 'not like to ') . 'receive news emails.';
这里我正在检查$_POST
变量是否实际设置,然后我们正在进行松散的比较检查以查看sub_me
是否为提交的值。
您还可以尝试将value
属性留空在表单元素上,然后只需使用$_POST['subscribed']
检查是否实际设置了isset($_POST['subscribed'])
。如果未选中该复选框,isset($_POST['subscribed'])
将返回false
。