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

时间:2016-05-28 18:18:26

标签: php html redirect contact-form

我希望在成功联系表单后将用户重定向回原始网站,但我也希望显示成功消息,因为当我的联系表单成功时,我会得到一个空白页面...感谢您提前寻求帮助.. < / p>

这是我的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();

  ?>

1 个答案:

答案 0 :(得分:0)

当您完成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