表单提交后重定向不起作用以感谢您的页面

时间:2019-12-20 07:24:24

标签: javascript php jquery

我有一个包含表单的索引页面,我想在表单提交后重定向页面以感谢您。电子邮件工作正常,我使用了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>

0 个答案:

没有答案