jquery ajax post没有重定向到php页面

时间:2017-01-12 15:02:36

标签: php jquery ajax

我的网站上有一个jquery ajax电话的联系表格,但我没有'当我点击按钮时看到.php页面。

这是ajax电话:

    var data = {
        name: $("input.userName").val(),
        email: $("input.userEmail").val(),
        tel: $('input.userPhone').val(),
        message: "Projet de "+$("input.userName").val() + ", Tel : " + $('input.userPhone').val(),
            body: body
        };

    $.ajax({
        type: "POST",
        url: "acerola.php",
        data: data,
        success: function(data){
            console.log('ajax sucessfully sent');
        }
    });

使用acerola.php:

<html>
<head><title>PHP Mail Sender</title></head>
<body>
<h1>OUIIiiii</h1>
<?php

echo "<h1>Coucou</h1>";

/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$name = $HTTP_POST_VARS['name'];
$email = $HTTP_POST_VARS['email'];
$tel = $HTTP_POST_VARS['tel'];
$message = $HTTP_POST_VARS['message'];
$body = $HTTP_POST_VARS['body'];

/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  echo "<h4>Invalid email address</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
  echo "<h4>No subject</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ( /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
    mail("plumerault@gmail.com", "Demande de devis web", $message . $body, "From:" . $email)
) {
  echo "<h4>Thank you for sending email</h4>";
  echo "<div>Thank you for sending email</div>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}

?>
</body>
</html>

我希望将用户重定向到acerola.php。但是用户仍然在原始页面上。

你能帮助我吗?

由于

1 个答案:

答案 0 :(得分:2)

在ajax响应之后,使用javascript启动重定向。

$.ajax({
    type: "POST",
    url: "acerola.php",
    data: data,
    success: function(data){
        console.log('ajax sucessfully sent');
        window.location.replace('URL HERE')
    }
});