簡體   English   中英

如果在MySQL表中找不到數據,如何清除文本框並使用Ajax發送警報消息?

[英]How to clear textboxes and send alert message using Ajax if the data can't be found in MySQL table?

我試圖清除文本框,並在MySQL表中找不到數據時發送警告消息。 有沒有辦法做到這一點,即使我使用類型:'json'? 因為當我試圖刪除json時,警報消息正在工作,問題是如果我這樣做它不顯示MySQL表中的數據。 提前感謝您的幫助。

 $(function() { $('#search').click(function() { var inp = $('#username'); if (inp.val().length > 0) { var src_uname = $('#username').val(); $.ajax({ url: "./search_process.php", type: "POST", dataType: "json", data: { username: src_uname }, success: function(data) { var fullname = data[0]['fullname']; var address = data[0]['address']; document.getElementById('fullname').value = fullname; document.getElementById('address').value = address; } }); } else { alert("Enter username in the textbox!"); } }); }); 
 <form id="form_data" style="width:40%;margin:1em auto;"> <div class="form-group"> <input type="text" id="username" name="username" class="form-control" placeholder="Enter Username" /> <input type="button" id="search" class="btn btn-success" value="Search" /> </div> <div class="form-group"> <input type="text" id="fullname" name="fullname" class="form-control" placeholder="Fullname" /> <input type="text" id="address" name="address" class="form-control" placeholder="Address" /> </div> </form> 

<?php
$cn = mysqli_connect("localhost","root","","testdb");

$username = $_POST['username'];
$query = "SELECT * FROM tblajax WHERE username = '$username' ";
$result = mysqli_query($cn,$query);
$numrows = mysqli_num_rows($result);

$info_arr = array();

if ($numrows > 0 ) {
    while ($rows = mysqli_fetch_assoc($result)) {
        $fullname = $rows['fullname'];
        $address = $rows['address'];

        $info_arr[] = array("fullname" => $fullname, "address" => $address);
    }

}
else {
    echo "<script>alert('Unable to find the information');</script>";
}
    echo json_encode($info_arr);
    exit;
?>

如果找不到數據,我想發送此警報消息:

echo "<script>alert('Unable to find the information');</script>";

如果無法找到數據,我還想清除這兩個文本框:

<input type = "text" id = "fullname" name = "fullname" class = "form-control" placeholder = "Fullname" />
<input type = "text" id = "address" name = "address" class = "form-control" placeholder = "Address" />

一種方法是在JSON中返回“成功”狀態。 然后,您可以在客戶端檢查它並根據需要提醒消息。 所以你可以改變你的代碼:

PHP

if ($numrows > 0 ) {
    $info_arr['data'] = array();
    while ($rows = mysqli_fetch_assoc($result)) {
        $fullname = $rows['fullname'];
        $address = $rows['address'];

        $info_arr['data'][] = array("fullname" => $fullname, "address" => $address);
    }
    $info_arr['success'] = true;
}
else {
    $info_arr = array('success' => false);
}
echo json_encode($info_arr);
exit;

jQuery的:

success: function(data) {

  if (data.success) {
      var fullname = data.data[0]['fullname'];
      var address = data.data[0]['address'];
      document.getElementById('fullname').value = fullname;
      document.getElementById('address').value = address;
  }
  else {
      document.getElementById('fullname').value = '';
      document.getElementById('address').value = '';
      alert('Unable to find the information');
  }
}

您應該在失敗時發送另一個HTTP狀態,例如404

if ($numrows > 0 ) {
    while ($rows = mysqli_fetch_assoc($result)) {
        $fullname = $rows['fullname'];
        $address = $rows['address'];

        $info_arr[] = array("fullname" => $fullname, "address" => $address);
    }

}
else {
    header("HTTP/1.0 404 Not Found");
}

並在JS中使用以下,類似於成功事件

success: function(data) {
          //var len = data.length;

          //if(len > 0){

          var fullname = data[0]['fullname'];
          var address = data[0]['address'];

          document.getElementById('fullname').value = fullname;
          document.getElementById('address').value = address;

          //}

        },
error: function() {
   document.getElementById('fullname').value = '';
   document.getElementById('address').value = '';

   alert ('Unable to find the information');
}

或檢查成功:function() whether data [0] ['fullname'];`

if (data[0]['fullname']) {
//do fill fields
} else {
 document.getElementById('fullname').value = '';
 document.getElementById('address').value = '';

 alert ('Unable to find the information');
}

暫無
暫無

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

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