简体   繁体   English

使用ajax在查询结果后重定向页面

[英]redirect page after query result using ajax

I would like to redirect the page if the condition inside the success will match the result of the test.php page that is "no_errors". 如果成功中的条件与“ no_errors”的test.php页面的结果匹配,我想重定向页面。

When the test.php page gives me an output of "no errors", the redirect function doesn't work. 当test.php页面显示“没有错误”的输出时,重定向功能不起作用。 Thanks in advance. 提前致谢。

$.ajax({
type: 'post',
url: 'test.php',
data: {user_name:tesseraval },
success: function (result) {       
    if(result === "no_errors") location.href = "ThankYou.html"
    else $( '#display_info' ).html(result);
}
});

test.php test.php

<?php
try {
  if (isset($_POST['user_name'])) {
    $user = $_POST['user_name'];
    $z = sprintf('%08d', $user);
    include("conn/auth.php");
    include("conn/db.php");
    include("conn/restriction.php");
    $query_string = "SELECT * FROM data WHERE user LIKE  '%".$z."%' ;";
    if ($result = $con->query($query_string)) {
      if ($result->num_rows > 0) {
        if ($result->num_rows > 1) {
          echo " Attention! Duplicated ";
          while ($row = $result->fetch_object()) {
            echo '<table  id="table" class ="table"><thead> <th> ID </th><th>Name</th><th> fullname </th></thead><tbody>';
            echo '<tr><td id ="cod">'.$row->Code.'</td>';
            echo '<td>'.$row->name.'</td>';
            echo '<td>'.$row->fullname.'</td>';
            echo '</tr></tbody></table>';
          }
        } else {
          $query_string2 = " SELECT * FROM blacklist WHERE user = ".$z." ;";
          if ($result2 = $con->query($query_string2)) {
            if ($result2->num_rows < 1) {
              echo "no_errors";
            } else {
              echo " BLACKLISTED cannot be used";
            }
          }
        }
      } else {
        echo " NO RESULT  ";
      }
    }
  }
} catch (Exception $e) {
  header('HTTP/1.1 500 Internal Server Error');
}
?>      

First thing I would do is place the redirect outside of if , just to see if its the if that is not triggering for whatever reason. 我要做的第一件事是将重定向置于if之外,只是为了查看其if出于某种原因而没有触发。 A good idea would also be to console.log(result) like CBroe said, both to see the result and also see if the success part of your ajax call even triggers. 像CBroe所说的那样,也最好是console.log(result) ,既可以查看结果,也可以查看ajax调用的success部分是否触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM