首页 > 解决方案 > 提交表单后重定向不起作用以感谢您的页面

问题描述

我有一个包含表单的索引页面,我希望页面在提交表单后重定向到谢谢。电子邮件工作正常,我使用了 phpmailer 和 smtp。下面我给出了示例 id。有人可以帮助如何重定向谢谢页。我已经尝试过标头位置和 url。但它不起作用。

Contact_me.php

$sender_name = isset($_POST['name']) ? $_POST['name'] : "";

    $sender_mobile = isset($_POST['mobile']) ? $_POST['mobile'] : "";

    // $sender_email = isset($_POST['email']) ? $_POST['email'] : "" ;

    $store = isset($_POST['store']) ? $_POST['store'] : "";

    $product = isset($_POST['product']) ? $_POST['product'] : "";

    $brand = isset($_POST['brand']) ? $_POST['brand'] : "";

    $experience = isset($_POST['experience']) ? $_POST['experience'] : "";

    $formname = isset($_POST['formname']) ? $_POST['formname'] : "";

    $popup = isset($_POST['popup']) ? $_POST['popup'] : "";

    //if($result){

    //Enable SMTP debugging. 
    $mail - > SMTPDebug = 3;
    //Set PHPMailer to use SMTP.
    $mail - > isSMTP();
    //Set SMTP host name                          
    $mail - > Host = "example.com";
    //Set this to true if SMTP host requires authentication to send email
    $mail - > SMTPAuth = true;
    //Provide username and password     
    $mail - > Username = "test@gmail.com";
    $mail - > Password = "admin@123";
    //If SMTP requires TLS encryption then set it
    $mail - > SMTPSecure = "tls";
    //Set TCP port to connect to 
    $mail - > Port = 587;

    $mail - > From = "test@gmail.com";
    $mail - > FromName = "test";

    $mail - > addAddress("test@gmail.com", "Recepient Name");

    $mail - > isHTML(true);

    $mail - > Subject = "Enquiry";
    $mail - > Body = "";
    $mail - > AltBody = "Alternative Message";

    if (!$mail - > send()) {
        echo "Mailer Error: ".$mail - > ErrorInfo;
        print json_encode(array('type' => 'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
        exit;
    } else {
        echo "Message has been sent successfully";

        print json_encode(array('type' => 'done', 'text' => 'Thank you for your email'));
        exit;
    }
    exit();
  ? >

Index.php 脚本

<script type="text/javascript">
            var allowed_file_size   = "1048576"; //1 MB allowed file size
            var allowed_file_types  = ['image/png', 'image/gif', 'image/jpeg', 'image/pjpeg', 'application/x-zip-compressed', 'application/pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']; //Allowed file types
            var border_color        = "#C2C2C2"; //initial input border color
            var maximum_files       = 2; //Maximum number of files allowed

            $("#quote-form").submit(function(e){
                e.preventDefault(); //prevent default action 
                proceed = true;

                //simple input validation
                $($(this).find("input[data-required=true], select[data-required=true]")).each(function(){
                        if(!$.trim($(this).val())){ //if this field is empty 
                            //$(this).css('border-color','red'); //change border color to red   
                            $(this).siblings().addClass('err-txt-active');
                            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   
                            $(this).siblings().addClass('err-txt-active');
                            proceed = false; //set do not proceed flag              
                        }   

                        var phone_reg = /^\d{10}$/; 
                        if($(this).attr("type")=="number" && !phone_reg.test($.trim($(this).val()))){
                           // $(this).css('border-color','red'); //change border color to red   
                            $(this).siblings().addClass('err-txt-active');
                            proceed = false; //set do not proceed flag              
                        }   
                }).on("input", function(){ //change border color to original
                     $(this).css('border-color', border_color);
                     $(this).siblings().removeClass('err-txt-active');
                });



                //if everything's ok, continue with Ajax form submit
                if(proceed){ 
                    var post_url = $(this).attr("action"); //get form action url
                    var request_method = $(this).attr("method"); //get form GET/POST method
                    var form_data = new FormData(this); //Creates new FormData object


                    //loading 
                    var d = document.getElementById("loading-icon");
                    d.className += "show-loading";


                    $.ajax({ //ajax form submit
                        url : post_url,
                        type: request_method,
                        data : form_data,
                        dataType : "json",
                        contentType: false,
                        cache: false,
                        processData:false
                    }).done(function(res){ //fetch server "json" messages when done
                        if(res.type == "error"){
                            $("#quote-form").html('<div class="error">'+ res.text +"</div>");

                        }
                        if(res.type == "done"){
                          //  alert('alert 1'); 
                            //$("#quote-form").html('<div class="success">'+ res.text +"</div>");
                            //setTimeout(function() {
                               // $('.success').fadeOut('fast');
                            //}, 2000);
                            setTimeout(function(){
                               // OK, finished jQuery staff, let's go redirect
                               window.location.href = "/thank-you.php";
                             },100);
                            $('#quote')[0].reset();
                            //$("#d-brochure").click();
                        }
                        else{
                        // alert('test');   
                        }
                    });
                }
            });

</script>

标签: javascriptphpjquery

解决方案


推荐阅读