表单成功后,jQuery Ajax setTimeout无法重定向? [英] Jquery ajax setTimeout after form succes not redirecting?

查看:92
本文介绍了表单成功后,jQuery Ajax setTimeout无法重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试提问之前,我尝试过出现一些问题,但没有机会使其生效. 当我将其添加到邮件发送成功警报时,它可以正常工作,但是我不想在php部分中添加脚本.

I tried questions showing up before asking a question didnt have chance to make it work. it works fine when I add it to mail send success alert, But I dont want to add script in php part.

我正在尝试将表单后的联系页面重定向到另一个页面 成功并延迟几秒钟.

I am trying to redirect contact page to an another page after form success and delay a few seconds.

这是我的Jquery ajax:

Here is my Jquery ajax :

$(document).ready(function(){
    $('#ContactForm').submit(function(event){
    event.preventDefault();
    var formValues = $(this).serialize();
        $.ajax({
        url:"modules/contact.inc.php",
        method:"POST",
        data:formValues,
        dataType:"JSON",
            success:function(data){
                if(data.error === 'ok'){
                    $('#result').html(data.error);
                    setTimeout(function() {
                        window.location = 'index.php';
                    }, 1000);
                } else {
                    $('#result').html(data.error);
                    $('#ContactForm')[0].reset();
                }
            }
        });
    });
});

我在成功功能中尝试了以下setTimeout();,但没有成功:

I tried the folowing setTimeout(); in success function but didnt work:

setTimeout(function() {
  window.location.replace("index.php");
},1000);

然后我尝试了:没有setTimeout函数的window.location.replace("index.php");也不起作用.

Then I tried : window.location.replace("index.php"); without setTimeout function didnt work too.

window.location.href
window.location.hostname
window.location

此内容在另一页中用于模态

This one works for modal in another page

setTimeout(function() {
window.location.reload();
}, 3000);

这些是我尝试的机会,谢谢您的建议和帮助.

These are my tries didnt have a chance, Thanks for any advice and help.

这是data.error包含的php部分:

Here is php part for data.error contain:

$error     = "";

    // Validate user name
    if(empty($_POST["fname"])){
        $error .= "<p class='error'>İsim girmediniz.</p>";
    } else {
        $name = test_input($_POST["fname"]);
    }   

    // Validate email address
    if(empty($_POST["email"])){
        $error .= "<p class='error'>E-Posta girmediniz.</p>";     
    } else{
        $email = $_POST["email"];
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error .= "<p class='error'>E-Posta doğru formatta değil.</p>";
        }
    }   

    // Validate user subject
    if(empty($_POST["subject"])){
        $error .= "<p class='error'>Konu girmediniz.</p>";
    } else {
        $subject = test_input($_POST["subject"]);
    }

        // Validate user message
    if(empty($_POST["message"])){
        $error .= "<p class='error'>Mesaj girmediniz.</p>";
    } else {
        $message = test_input($_POST["message"]);
    }

    // Validate user departman
    if(empty($_POST["departmant"])){
        $error .= "<p class='error'>departman Seçin.</p>";
    } else {
        $departman = test_input($_POST["departmant"]);
    }
    if($error === ''){
        require "../PHPMailer/mailer.php";
        $mail = new mailSend();
        $email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
        $name = test_input($_POST["fname"]);
        $subject = test_input($_POST["subject"]);
        $departman = test_input($_POST["departmant"]);
        $message = test_input($_POST["message"]);
        $error = $mail->sendMail($email,$name,$subject,$departman,$message); 
    }else{
        $error .= "<p class='error'>Formda bir hata oluştu.</p>";
    }
$data = array(
 'error'  => $error
);

echo json_encode($data);

成功了,谢谢您的回答,

EDIT : Got it work thanks for answers,

显示错误导致问题,$('#result').html(data.error);我将其更改为文本消息,而不是来自php的成功消息:

Displaying error causing the problem, $('#result').html(data.error); I changed it to text message instead of success message from php:

$('#result').html('Form successfuly');
 $('#ContactForm')[0].reset();
 setTimeout(function() {
   window.location = 'index.php';
 }, 1000);

它工作正常.

推荐答案

String.replace()需要两个参数.如所写,它将查找字符串"index.php",并将其替换为任何内容.尝试添加正则表达式以匹配所有内容,并替换为新的URL.

String.replace() requires two parameters. As written, it will look for the string "index.php" and replace it with nothing. Try adding a regex to match everything and replace with your new URL.

setTimeout(function() {
  window.location.replace( /.*/, "index.php");
},1000);

使用完整的网址(即https://yourdomain.com/index.php)或编写更好的正则表达式.例如,如果您的域名以 .com 结尾,则可以执行以下操作:

Use the full URL (i.e. https://yourdomain.com/index.php) or write a better regex. For instance, if your domain ends with .com, you could do something like:

setTimeout(function() {
  window.location.replace( /\.com\/.*/, ".com/index.php");
},1000);

这篇关于表单成功后,jQuery Ajax setTimeout无法重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆