简体   繁体   English

如果在MySQL表中找不到数据,如何清除文本框并使用Ajax发送警报消息?

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

I am trying to clear the textboxes and send an alert message when the data can't be found inside the MySQL table. 我试图清除文本框,并在MySQL表中找不到数据时发送警告消息。 Is there a way to do this even I use the type: 'json'? 有没有办法做到这一点,即使我使用类型:'json'? Because when I tried to remove json, the alert message is working, problem is if I do this it doesn't show the data inside MySQL table. 因为当我试图删除json时,警报消息正在工作,问题是如果我这样做它不显示MySQL表中的数据。 Thank you in advance for the help. 提前感谢您的帮助。

 $(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;
?>

I want to send this alert message if the data can't be found: 如果找不到数据,我想发送此警报消息:

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

And also I want to clear these two textboxes if the data can't be found: 如果无法找到数据,我还想清除这两个文本框:

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

One way to do this is to return a "success" status in your JSON. 一种方法是在JSON中返回“成功”状态。 Then you can check that on the client side and alert a message as required. 然后,您可以在客户端检查它并根据需要提醒消息。 So you could change your code like this: 所以你可以改变你的代码:

PHP 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: 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');
  }
}

You should send another HTTP status on fail, eg 404 您应该在失败时发送另一个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");
}

and use below in JS, similar to the success event 并在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');
}

or check in success: function() whether data[0]['fullname'];` exists 或检查成功: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