我在邮件发送后使用重定向的联系表单,但我希望用户收到一条“已发送邮件”的提醒信息。而是在联系表格的某处消失。这意味着页面永远不会刷新,用户也不会从页面或联系表单中删除。
这是可能的吗?如果是这样,有人可以告诉我我需要添加到下面的当前代码......
<form id="contact-form-face" class="clearfix" action="http://www.demo.com/php/contactengine.php">
<input type="text" name="email" value="Email" onFocus="if (this.value == 'Email') this.value = '';" onBlur="if (this.value == '') this.value = 'Email';" />
<textarea name="message" onFocus="if (this.value == 'Message') this.value = '';" onBlur="if (this.value == '') this.value = 'Message';">Message</textarea>
<input class="contact_btn" name="submit" type="submit" value="Send Message" />
</form>
PHP:
<?php
$EmailFrom = "myemail";
$EmailTo = "myemail";
$Subject = "";
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
任何帮助都会非常感激,因为我对这方面很陌生。
答案 0 :(得分:3)
您只需使用AJAX即可:
$(function () {
$("#contact-form-face").submit(function (e) {
e.preventDefault();
$.post($(this).attr("action"), $(this).serialize(), function () {
alert("Submitted!");
});
});
});
如果网址位于其他域中,则会阻止CORS。为了避免这种情况,请使用:
<?php
header("Access-Control-Allow-Origin: *");
在PHP文件上,正在调用它。
表格互动:
$(function () {
$("#contact-form-face").submit(function (e) {
e.preventDefault();
$.post($(this).attr("action"), $(this).serialize(), function () {
$("#post-ajax").html("Your Message Has Been Sent! Thank You For Contacting Us.").show();
$("#contact-form-face").hide();
});
});
});
&#13;
<!-- CONTACT FORM -->
<div class="span9 contact_form">
<div id="note"></div>
<div id="fields">
<div id="post-ajax" style="display: none;"></div>
<form id="contact-form-face" class="clearfix" action="http://www.demo.com/php/contactengine.php">
<input type="text" name="email" value="Email" onFocus="if (this.value == 'Email') this.value = '';" onBlur="if (this.value == '') this.value = 'Email';" />
<textarea name="message" onFocus="if (this.value == 'Message') this.value = '';" onBlur="if (this.value == '') this.value = 'Message';">Message</textarea>
<input class="contact_btn" name="submit" type="submit" value="Send Message" />
</form>
</div>
</div><!-- //CONTACT FORM -->
&#13;