停止表单post php脚本打开新页面

停止表单post php脚本打开新页面,php,html,forms,Php,Html,Forms,我有一个在几个页面上都相同的联系人表单,所以我有一个php脚本,所有具有联系人表单的页面都会调用它。脚本运行良好,处理recaptcha,发送电子邮件,一切都很好。但是,每次它运行时,在通知用户邮件发送成功后,它会打开一个空白页面,其中包含php脚本的URL。我只想让页面保持原样——显示成功(或失败)警报,用户单击ok,他就盯着同一个页面。以下是表格: <div class="col-md-7">

我有一个在几个页面上都相同的联系人表单,所以我有一个php脚本,所有具有联系人表单的页面都会调用它。脚本运行良好,处理recaptcha,发送电子邮件,一切都很好。但是,每次它运行时,在通知用户邮件发送成功后,它会打开一个空白页面,其中包含php脚本的URL。我只想让页面保持原样——显示成功(或失败)警报,用户单击ok,他就盯着同一个页面。以下是表格:

                    <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>
但这是我看到的。 单击“发送”之前: 处理完表格后:

只要我在警报上单击ok,我就会看到一个空白页面,其中的地址栏显示php脚本的位置

只需在其他页面上重定向用户

使用
header()
函数

脚本结束后,只需将用户重定向到其他页面,如可能包含感谢消息的
thank.php
,或成功发送的
mail\u.php

它是通过在php中使用来实现的

它是这样使用的

header('Location:mail_sent.php')


因此,添加这一行,用户将重定向到
mail_sent.php
页面。

您可以使用location.href重定向用户。因此,在警报代码之后添加:
location.href=“{url\u of_your\u form\u page}”


您应该避免使用header函数,因为它会向浏览器发送http位置头。这将导致浏览器重定向到url,而不显示警报弹出窗口。

您理解@cpeddie,或者需要进一步澄清?尝试了此操作,但无效。请参阅更新的问题。您可以研究如何使用Ajax。然后您可以在不离开页面的情况下发布表单。我也希望你有自己的文件中的联系形式,你包括在所有的网页,你想要它,而不是有它的多个副本?完美!这正是我需要知道的。谢谢你的帮助!!
<?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>"; 
      }
    }
 ?>
header('Location:http://sq36.cawgcap.org');