发送邮件后重定向到感谢页面


in different code After sending mail Redirect to thank you page

我想在将邮件发送到活动页面后重定向页面。下面是我的PHP联系人表单页面。现在我想在发送邮件后重定向到我的"谢谢"页面。

//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    $output = json_encode(array( //create JSON data
        'type'=>'error', 
        'text' => 'Sorry Request must be Ajax POST'
    ));
    die($output); //exit script outputting json data
} 
//Sanitize input data using PHP filter_var().
$user_name      = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
$user_email     = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
$phone_number   = filter_var($_POST["phone_number"], FILTER_SANITIZE_NUMBER_INT);
$message        = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error.
    $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
    die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
    $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
    die($output);
}
if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field
    $output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number'));
    die($output);
}
if(strlen($message)<3){ //check emtpy message
    $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
    die($output);
}
//email body
$message_body = $message."'r'n'r'n"
      .$user_name."'r'nEmail : "
      .$user_email."'r'nPhone Number : ". $phone_number ;
//proceed with PHP email.
$headers = 'From: '.$from_email.'' . "'r'n" .
'Reply-To: '.$user_email.'' . "'r'n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
 if(!$send_mail)
{
    //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
    $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
    die($output);
}else{
    $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .', your message has been sent. Thank you!'));
    die($output);
}
}
?>

在HTML页面中,此代码

<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var _scroll = true, _timer = false, _floatbox = $("#contact_form"), _floatbox_opener = $(".contact-opener");
        _floatbox.css("right", "-322px"); //initial contact form position
        //Contact form Opener button
        _floatbox_opener.click(function () {
            if (_floatbox.hasClass('visiable')) {
                _floatbox.animate({ "right": "-322px" }, { duration: 300 }).removeClass('visiable');
            } else {
                _floatbox.animate({ "right": "0px" }, { duration: 300 }).addClass('visiable');
            }
        });
        //Effect on Scroll
        $(window).scroll(function () {
            if (_scroll) {
                _floatbox.animate({ "top": "30px" }, { duration: 300 });
                _scroll = false;
            }
            if (_timer !== false) { clearTimeout(_timer); }
            _timer = setTimeout(function () {
                _scroll = true;
                _floatbox.animate({ "top": "10px" }, { easing: "linear" }, { duration: 500 });
            }, 400);
        });

        //Ajax form submit
        $("#submit_btn").click(function () {
            var proceed = true;
            //simple validation at client's end
            //loop through each field and we simply change border color to red for invalid fields       
            $("#contact_form input[required=true], #contact_form textarea[required=true]").each(function () {
                $(this).css('border-color', '');
                if (!$.trim($(this).val())) { //if this field is empty 
                    $(this).css('border-color', 'red'); //change border color to red   
                    proceed = false; //set do not proceed flag
                }
                //check invalid email
                var email_reg = /^(['w-'.]+@(['w-]+'.)+['w-]{2,4})?$/;
                if ($(this).attr("type") == "email" && !email_reg.test($.trim($(this).val()))) {
                    $(this).css('border-color', 'red'); //change border color to red   
                    proceed = false; //set do not proceed flag              
                }
            });
            if (proceed) //everything looks good! proceed...
            {
                //get input field values data to be sent to server
                post_data = {
                    'user_name': $('input[name=name]').val(),
                    'user_email': $('input[name=email]').val(),
                    'phone_number': $('input[name=phone2]').val(),
                    'msg': $('textarea[name=message]').val()
                };
                //Ajax post data to server
                $.post('contact-form.php', post_data, function (response) {
                    if (response.type == 'error') { //load json data from server and output message     
                        output = '<div class="error">' + response.text + '</div>';
                    } else {
                        output = '<div class="success">' + response.text + '</div>';
                        //reset values in all input fields
                        $("#contact_form  input[required=true], #contact_form textarea[required=true]").val('');
                    }
                    $("#contact_form #contact_results").hide().html(output).slideDown();
                }, 'json');
            }
        });
        //reset previously set border colors and hide all message on .keyup()
        $("#contact_form  input[required=true], #contact_form textarea[required=true]").keyup(function () {
            $(this).css('border-color', '');
            $("#result").slideUp();
        });
    });
</script>
<div class="" id="contact_form">
    <div id="contact_results" style="color:#000"></div>
    <div id="contact_body">
        <label>
            <input type="text" name="name" id="name" required="true" class="form-control" placeholder="Your full name*">        
        </label>
        <label>
            <input type="text" name="phone2" maxlength="15" required="true" class="form-control" id="phone2" placeholder="Your Contact No.*">           
        </label>
        <label>
            <input type="email" name="email" required="true" class="form-control" id="Email1" style="width:404px" placeholder="Your e-mail*">            
        </label>
        <div class="form-group">
            <textarea class="form-control" name="message" rows="4" placeholder="Write your comment here*"></textarea>               
        </div>
        <label>
            <span>&nbsp;</span><input class="btn btn-primary" type="submit" id="submit_btn" value="Submit">
        </label>
    </div>
</div>

如何解决这个问题???请我需要帮助。。。请帮我快点。。看待gaurav patel

您在发布此票证之前搜索过吗?-如何在PHP中进行重定向?

只需简单地写

 header("Location: http://example.com/thank-you.php");
 exit;

而不是你的

$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);

使用:

$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .', your message has been sent. Thank you!'));
header("Location: http://example.com/thankyou.php");
die($output);//or simply die();

使用

header('location: http://stackoverflow.com/success.php');

或者像这个一样使用html重定向

<meta http-equiv="refresh" content="0; url=http://stackoverflow.com/success.php" />