表单成功后,Jquery ajax setTimeout 没有重定向? [英] Jquery ajax setTimeout after form succes not redirecting?

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

问题描述

在提出问题之前我尝试过出现问题,但没有机会让它发挥作用.当我将它添加到邮件发送成功警报时它工作正常,但我不想在 php 部分添加脚本.

<块引用>

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

这是我的 Jquery ajax:

$(document).ready(function(){$('#ContactForm').submit(function(event){event.preventDefault();var formValues = $(this).serialize();$.ajax({url:"modules/contact.inc.php",方法:POST",数据:形式值,数据类型:JSON",成功:功能(数据){if(data.error === 'ok'){$('#result').html(data.error);设置超时(功能(){window.location = 'index.php';}, 1000);} 别的 {$('#result').html(data.error);$('#ContactForm')[0].reset();}}});});});

我在成功函数中尝试了以下 setTimeout(); 但没有奏效:

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

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

window.location.href窗口.位置.主机名窗口位置

这个适用于另一个页面的模态

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

这些是我没有机会的尝试,感谢您的任何建议和帮助.

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

$error = "";//验证用户名如果(空($_POST[fname"])){$error .= "<p class='error'>İsim girmediniz.</p>";} 别的 {$name = test_input($_POST["fname"]);}//验证电子邮件地址如果(空($_POST[电子邮件"])){$error .= "<p class='error'>E-Posta girmediniz.</p>";} 别的{$email = $_POST["email"];if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {$error .= "<p class='error'>E-Posta doğru formatta değil.</p>";}}//验证用户主题如果(空($_POST[主题"])){$error .= "<p class='error'>Konu girmediniz.</p>";} 别的 {$subject = test_input($_POST["subject"]);}//验证用户消息如果(空($_POST[消息"])){$error .= "<p class='error'>Mesaj girmediniz.</p>";} 别的 {$message = test_input($_POST["message"]);}//验证用户部门如果(空($_POST[部门"])){$error .= "<p class='error'>departman Seçin.</p>";} 别的 {$departman = test_input($_POST["departmant"]);}if($error === ''){需要../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);}别的{$error .= "<p class='error'>Formda bir hata oluştu.</p>";}$数据=数组('错误' =>$错误);回声 json_encode($data);

<块引用>

得到它的工作感谢您的回答,

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

$('#result').html('表格成功');$('#ContactForm')[0].reset();设置超时(功能(){window.location = 'index.php';}, 1000);

效果很好.

解决方案

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

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

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

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

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.

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();
                }
            }
        });
    });
});

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

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

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.

EDIT: 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,

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);

it works fine.

解决方案

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);

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天全站免登陆