如何防止PHP文件在提交联系表格后弹出
How do I prevent PHP file from popping up after submitting contact form?
现在,每当我点击网站上联系人表单上的提交按钮时,它都会将我带到www.mysite.com/php/function/email.php页面,而不是像我希望的那样在后台运行email.php。
该网站应该允许您提交联系人表格,然后按钮会逐渐消失,并在"您的消息已发送!"中淡入,然后淡入按钮。
HTML:
<form name="contact form" id='contact_form' method="post" action="php/function/email.php">
<div class="row half">
<div class="6u">
<input name="name" placeholder="Name" type="text" class="text" />
</div>
<div class="6u">
<input name="email" placeholder="Email" type="email" class="text" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row half">
<div class="12u">
<input id="submit" name="submit" type="submit" value="Send Message" class="button button-icon icon icon-envelope" />
</div>
</div>
<p id="success" style="display:none">Your message has been sent! I'll get back to you soon!</p>
<p id="error" style="display:none">Please fill in all the fields and try again!</p>
JS:
$(document).ready(function(){
$('#send_message').click(function(e){
//Stop form submission & check the validation
e.preventDefault();
// Variable declaration
var name = $('#name').val();
var email = $('#email').val();
var subject = $('#subject').val();
var message = $('#message').val();
// Disable submit button just after the form processed 1st time successfully.
$('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });
/* Post Ajax function of jQuery to get all the data from the submission of the form as soon as the form sends the values to email.php*/
$.post("email.php", $("#contact_form").serialize(),function(result){
//Check the result set from email.php file.
if(result == 'sent'){
//If the email is sent successfully, remove the submit button
$('#submit').remove();
//Display the success message
$('#success').fadeIn(500);
}else{
//Display the error message
$('#error').fadeIn(500);
// Enable the submit button again
$('#submit').removeAttr('disabled').attr('value', 'Send The Message');
}
});
}
});
};
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Example';
$to = 'example@gmail.com';
$subject = 'Example Contact Form';
$body = "
From: $name
Email: $email
---------------
Message: 'n
$message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo 'sent';
} else {
echo 'failed';
}
}
?>
<input type="submit">
的.click()
事件实际上并不是导致重定向的原因。相反,它在<form>
处变为.submit()
事件。
你可以选择e.stopPropagation()
,这样它就不会到达<form>
。
//Stop form submission & check the validation
e.stopPropagation();
或者<form>
的.submit()
的preventDefault()
。
$('#contact_form').submit(function (e) {
e.preventDefault();
});
注意:您还应该考虑将代码移动到.submit()
处理程序中,因为大多数浏览器支持提交<form>
的其他方式,而不是实际单击<input type="submit">
。
问题是您在#send_message
上查找点击事件,但提交按钮的id为submit
。所以点击事件永远不会发生。
相关文章:
- 我如何使此联系表格工作
- Form获胜'没有ID主联系表格无法验证
- 联系表格(具体选择)和电子邮件
- 联系表格提交两次
- 我的联系表格在测试/PHPMailer后给了我一个内部服务器错误(500)
- PHP电子邮件联系表格
- 联系表格帮助不大
- 在不离开页面和bootstrapValidator的情况下发送联系表格
- 联系表格html5
- 使用Javascript的联系表格
- 联系表格不发送电子邮件
- 联系表格未提交
- 无法提交我的 ajax 联系表格
- 温泉UI:联系我们 表格提交
- 联系表格不发送电子邮件,也没有通知
- 联系表格 7 dom 操作
- JavaScript 不适用于联系我们表格
- 联系表格不接受希腊字符
- 联系表格收集数据而不是发布
- WordPress联系表格7重定向单选按钮值不起作用