簡體   English   中英

jquery ajax 帖子不會重定向到 php 頁面

[英]jquery ajax post doesn't redirect to php page

我的網站上有一個帶有 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("plu@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。 但是用戶停留在原始頁面上。

你能幫助我嗎 ?

謝謝

在 ajax 響應之后,使用 javascript 觸發重定向。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM