Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Ajax响应上添加if-else函数_Php_Ajax - Fatal编程技术网

Php 在Ajax响应上添加if-else函数

Php 在Ajax响应上添加if-else函数,php,ajax,Php,Ajax,我试图根据我的回复重定向到一个页面。但我做错了。我试过答案 但不起作用。下面是我的Ajax脚本 success: function (response) { if (response === 'success') { window.location.replace("home"); } else { $(".error_message").fadeIn(); $(".e

我试图根据我的回复重定向到一个页面。但我做错了。我试过答案 但不起作用。下面是我的Ajax脚本

   success: function (response) {
      if (response === 'success') {
        window.location.replace("home");
      } else {
        $(".error_message").fadeIn();
        $(".error_message").html("");
        $(".error_message").html(response);
        $(".error_message").fadeOut(5000);
      }
    }
下面是我的php代码

            if ($stmt->execute()) {
                echo "success";
            }
            $stmt->close();
它在表单成功提交后回显消息

但我希望它重新加载,而不是停留在同一页上。

尝试以下操作:

阿贾克斯:

PHP:


我更改了PHP,使其发送JSON响应而不是纯文本,此外,它还位于一个数组中,该数组有一个成功字段,JavaScript将检查该字段是否正确。

谢谢,但仍然保持在同一页面上。@Everythinggood您是否希望重新加载或jquery替换现有html?谢谢您的帮助。我希望它是重新加载。请尝试在if语句中添加警报,以查看是否从中得到响应。我更新了我的答案,如果你想离开的话。基本上,我是想看看这个回答是否正确。如果它没有弹出警报,那么我将把警报移到If-else语句上方,并向response.success变量发出警报,查看它返回了什么。感谢您的帮助。我的php代码错了。就这么过去了
   success: function (response) {
      if (response.success) {
        alert(response);
        location.reload();
      } else {
        $(".error_message").fadeIn();
        $(".error_message").html("");
        $(".error_message").html(response);
        $(".error_message").fadeOut(5000);
      }
    }
if ($stmt->execute()) {
    return json_encode(array("success" => true));
}
$stmt->close();