[英]Contact form is sending email but not responding after submission.
I have my contact form set where after submitting it redirects back to my homepage but for some reason it just stays on the same page. 我设置了联系表单,提交后将其重定向到我的主页,但由于某种原因,它只停留在同一页面上。 I'm trying to receive confirmation after email is sent and then a redirect back to my homepage. 发送电子邮件后,我试图接收确认,然后重定向回我的主页。 I'm using php and javascript........................................... 我正在使用php和javascript ....................................................
<div class="col-sm-6 col-sm-offset-3">
<h3>Send me a message</h3>
<form role="form" id="contactForm" action="index2.php" method="POST">
<div class="row">
<div class="form-group col-sm-6">
<label for="name" class="h4">Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" required>
</div>
<div class="form-group col-sm-6">
<label for="email" class="h4">Email</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" required>
</div>
</div>
<div class="form-group">
<label for="message" class="h4 ">Message</label>
<textarea id="message" class="form-control" rows="5"
placeholder="Enter your message" required></textarea>
</div>
<button type="submit" id="form-submit" class="btn btn-primary btn-lg
pull-right ">Submit</button>
<div id="msgSubmit" class="h3 text-center hidden">Message Submitted!</div>
</form>
index2.php index2.php
<meta http-equiv="refresh" content="0; url=http://myurl.com/" />
</header>
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$EmailTo = "arash281pro@live.com";
$Subject = "New Message Received";
// prepare email body text
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
echo "success";
}else{
echo "invalid";
}
?>
js js
$("#contactForm").submit(function(event){
// cancels the form submission
event.preventDefault();
submitForm();
});
function submitForm(){
// Initiate Variables With Form Content
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "index2.php",
data: "name=" + name + "&email=" + email + "&message=" + message,
success : function(text){
if (text == "success"){
formSuccess();
}
}
});
}
function formSuccess(){
$( "#msgSubmit" ).removeClass( "hidden" );
}
var confirmSubmit = true;
$('form').submit(function(e) {
if (confirmSubmit) {
e.stopPropagation();
if (confirm('Are you sure you want to send this form?')) {
confirmSubmit = false;
$('form').submit();
}else{
alert("The form was not submitted.");
}
}
});
You're not actually redirecting back to your homepage. 您实际上并没有重定向回到首页。 For that you'll need to add something like this after your submit function: 为此,您需要在Submit函数之后添加以下内容:
window.location.replace("index2.php");
Rather than using <meta http-equiv="refresh" content="0; url=http://myurl.com/" />
in index2.php, just add header("Location: http://myurl.com/");
而不是在index2.php中使用<meta http-equiv="refresh" content="0; url=http://myurl.com/" />
,只需添加header("Location: http://myurl.com/");
<meta http-equiv="refresh" content="0; url=http://myurl.com/" />
header("Location: http://myurl.com/");
at the end of the script instead of "success" or "invalid" message. 在脚本末尾而不是“成功”或“无效”消息。 Also remove any HTML before the PHP code starts to prevent errors like "Headers already sent, output started on line ..." 在PHP代码开始之前,还要删除所有HTML,以防止出现诸如“标题已发送,输出开始于行...”之类的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.