2017-09-05 36 views
0

如果成功內部的條件與test.php頁面的結果是「no_errors」相匹配,我想重定向頁面。使用ajax查詢結果後重定向頁面

當test.php頁面給我輸出「沒有錯誤」時,重定向功能不起作用。提前致謝。我會做

$.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的

<?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'); 
} 
?>  
+4

'console.log(result)'顯示了究竟是什麼? – CBroe

+1

嘗試它'window.location.href' –

+0

可能重複[如何重定向到另一個網頁?](https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage) – Alexander

回答

0

的第一件事是將重定向的if之外,只是爲了看看它是不是觸發不論什麼原因,if。一個好主意也會像CBroe所說的console.log(result)一樣,既看到結果,也看到你的ajax調用的success部分是否觸發。