成功联系表单 PHP 后将用户重定向回原始站点并获得成功消息

Redirect user back to original site after successful contact form PHP and get successful message

我想在成功联系表单后将用户重定向回原始站点,但我也想显示一条成功消息,因为当我的联系表单成功时,我得到一个空白页面...提前感谢您的帮助。 .

这是我的 html 表格:

   <form id="form" class="blocks" action="validateform.php" method="post" onkeyup="foo();" name="newForm">
                <p style="color: #ff0000;padding: 0 0 0 6.4%">Required fields(*)</p>
        <p>
            <label>Name *:</label>
            <input type="text" class="text" name="name" />
        </p>
        <p>
            <label>E-mail *:</label>
            <input type="text" class="text" name="email" />
        </p>
        <p>
            <label>Subject  :</label>
            <input type="text" class="text" name="phone" />
        </p>
        <p class="area">
            <label>Message *:</label>
            <textarea class="textarea" name="message"></textarea>
        </p>
        <p>
            <label>&nbsp;</label>
            <input type="submit" class="btn" value="Send" />
        </p>
    </form>

这是我的 php:

    <?php
 if(isset($_POST['email'])) {

$email_to = "mymail"; 

$email_subject = "";

    function died($error) {
    // edw mpainei to mhnuma p tha emfanisei se periptwsh error
    echo "We are very sorry, but we find some error(s) in your form<br/>      <br/>";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
  }

   if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['message'])) {      
    }

$name = $_POST['name']; 
$email_from = $_POST['email']; 
$message = $_POST['message'];

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email_from)) {
       $error_message .= '<br>The Email Address you entered does not appear to be   valid.<br />';
   }
   $string_exp = "/^[A-Za-z .'-]+$/";
   if(!preg_match($string_exp,$name)) {
   $error_message .= '<br>The First Name you entered does not appear to be   valid.<br />';
 }
   if(strlen($message) < 2) {
   $error_message .= '<br>The Comments you entered do not appear to be valid.<br />';
 }
    if(strlen($error_message) > 0) {
   died($error_message);
    }
       $email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
     }

    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
     $email_message .= "Message: ".clean_string($message)."\n";

   $headers = 'From: '.$email_from."\r\n".
 'Reply-To: '.$email_from."\r\n" .
  'X-Mailer: PHP/' . phpversion();
  mail($email_to, $email_subject, $email_message, $headers);  
  ?>

   <?php
   }
     die();

  ?>

当您完成 PHP 联系表格邮寄代码时,此时您的代码只显示:

die();

改为:

die('<meta http-equiv="refresh" content="0; url=http://example.com?bob=youruncle" />');

当然,将 bob=uncle key=value 更改为您想要的任何内容 - 无意义的字符串,或 contact=sent,或其他任何内容。

然后,在 index.php 文件的顶部,添加:

<?php

    if ( isset($_GET['bob'] ){
        $message = 'Contact form sent!';
    }else{
        $message = '';
    }

javascript/jQuery 页面:

$(document).ready(function(){

    var sendMsg = "<?php echo $message; ?>";
    if (sendMsg.length){
        alert(sendMsg);
    }

}); //END document.ready

或者,更花哨:

$(document).ready(function(){

    var sendMsg = "<?php echo $message; ?>";
    if (sendMsg.length){
        setTimeout(function(){
            $('body').append('<div id="thx" style="display:none;position:absolute;top:20%;left:30%;font-size:5rem;background:wheat;z-index:2;">Thanks for contacting us!</div>');
            $('#thx').fadeIn(700,function(){
                setTimeout(function(){
                    $('#thx').fadeOut(700);
                },3000);
            });
        },3000);
    }

}); //END document.ready