Jquery ajax setTimeout after form succes not redirecting?

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