主页面:
<head> <script src="../jquery-2.2.3.min.js"></script> </head><body> <div> <input type="text" id="uid" /><span id="xinxi"></span> </div> </body><script type="text/javascript"> $(document).ready(function(e) {$("#uid").blur(function(){//把文本框的值取出来var uid = $(this).val();//调用ajax$.ajax({url:"ChuLi.php",//处理页面data:{n:uid}, //传递的数据,JSON数据type:"POST", //提交方式datatype:"TEXT", //返回数据的类型success:function(data){ //回调函数if(data=="OK"){var str = "该水果名可以添加!";$("#xinxi").html(str);}else{var str = "<span style='color:red'>该水果名已经存在!</span>";$("#xinxi").html(str);}}});})}); </script>
处理页面:
<?php //接收传递过来的参数 $name = $_POST["n"]; //查数据库 include("../DBDA.class.php"); $db = new DBDA();$sql = "select count(*) from fruit where Name='{$name}'";$attr = $db->Query($sql);if($attr[0][0] == 1) {echo "NO"; } else {echo "OK"; }