我有一个在几个页面上相同的联系表单,所以我有一个php脚本,由具有联系表单的所有页面调用。脚本工作正常,处理recaptcha,发送电子邮件,一切都很好。但是,每次运行时,在告知用户邮件发送成功的警报之后,它会打开一个空白页面,其中包含php脚本的URL。我只希望页面保持原状 - 显示成功(或失败)警报,用户点击确定,并且他正盯着同一页面。这是以下形式:
<div class="col-md-7">
<h2>Contact Us</h2> Got a question ? Feedback? Awesome!
<form class="mt-4 mt-md-0" role="form" action="/php/mail.php" method="post" >
<div class="form-group">
<label for="name" class="sr-only control-label">Name</label>
<input class="form-control bg-faded-4" id="form_name" type="text" name="name" placeholder="Please enter your name *" required="required" data-error="Name is required.">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="email" class="sr-only control-label">Email</label>
<input class="form-control bg-faded-4" id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="message" class="sr-only control-label">Message</label>
<textarea class="form-control bg-faded-4" id="form_message" name="message" placeholder="Message for me *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<div class="row" style="margin-bottom:30px;">
<span class="msg-error error"></span>
<div class="g-recaptcha" data-sitekey="<MY_SITE_KEY>" style="padding-left: 15px"></div>
</div>
</div>
<button type="submit" class="btn btn-secondary btn-sm mt-2" name="submit" id="submit">Send Message</button>
</form>
</div>
</div>
这是php脚本:
<?php
$errName = "";
$errEmail = "";
$errMessage = "";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'myform@mysite.com';
$to = 'myemail@gmail.com';
$subject = 'Message from Contact Form ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
else
$sender_name = stripslashes($_POST["name"]);
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
else
$sender_email = stripslashes($_POST["email"]);
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
else
$sender_message = stripslashes($_POST["message"]);
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => '<MY_SECRET_KEY>',
'response' => $_POST["g-recaptcha-response"]
);
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
// If there are no errors, send the email
if ($captcha_success->success==false) {
echo "<p>You are a bot! Go away!</p>";
} else if ( ($errName != "") ) {
echo "<script type='text/javascript'>alert('Please enter your name.'); </script>";
} else if ($errEmail != "") {
echo "<script type='text/javascript'>alert('You must enter a valid email address. Please try again.');</script>";
} else if ( $errMessage != "") {
echo "<script type='text/javascript'>alert('You must enter some message text. Please try again.');</script>";
} else {
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
// Send email
$mailResponse = mail($to, $subject, $message, implode("\n", $headers));
if ( $mailResponse == 1 ) {
echo "<script type='text/javascript'>alert('We\'ll be in touch!'); </script>";
} else {
echo "<script type='text/javascript'>alert('Error sending message. Please try again.');</script>";
}
}
?>
我确定这是非常丑陋的PHP代码,并没有真正完成很多PHP编程。但我真正的问题是这个空白窗口在php文件处理表单后显示 - 我该如何阻止它?
感谢.....
所以php脚本的最后一行现在是:
header('Location:http://sq36.cawgcap.org');
但这就是我所看到的。 在点击“发送”之前: 并在表单处理完毕后:
只要我在警报上点击确定,我就会盯着一个空白页面,地址栏显示php脚本的位置。
答案 0 :(得分:0)
只需将用户重定向到其他某个页面即可。
使用header()
功能。
当脚本结束时,只需将用户重定向到其他某个页面,例如可能包含感谢信息的thanks.php
或mail_sent_successfully.php
。
通过在php中使用header()
function来实现。
像这样使用
header('Location: mail_sent.php');
因此,添加此行,用户将重定向到mail_sent.php
页面。
答案 1 :(得分:0)
您可以使用 location.href 重定向用户。因此,在提醒代码后添加: location.href="{url_of_your_form_page}";
。
您应该避免使用标头功能,因为它会向浏览器发送http位置标头。它将导致浏览器重定向到URL而不显示警告弹出窗口。