ajax注册与添加

 1 <body>
 2 
 3 <input type="text" id="uid" /><span id="ts"></span>
 4 
 5 </body>
 6 <script type="text/javascript">
 7 
 8 $("#uid").blur(function(){
 9         //取出用户名
10         var uid = $(this).val();
11         
12         //去数据库进行匹配
13         $.ajax({
14                 url:"ajax2.php", //处理页面的路径
15                 data:{u:uid}, //要提交的数据是一个JSON
16                 type:"POST", //提交方式
17                 dataType:"TEXT", //返回数据的类型
18                 //TEXT字符串 JSON返回JSON XML返回XML
19                 success:function(data){ //回调函数
20                         if(data.trim()=="OK")
21                         {
22                             $("#ts").html("该用户名可用");
23                             $("#ts").css("color","green");
24                         }
25                         else
26                         {
27                             $("#ts").html("该用户名不可用");
28                             $("#ts").css("color","red");
29                         }
30                     }
31             });
32     })
33 
34 </script>
35 </html>
<?php
$uid = $_POST["u"];
require "DBDA.class.php";
$db = new DBDA();

$sql = "select count(*) from people where uid='{$uid}'";

$arr = $db->query($sql);

if($arr[0][0])
{
	echo "NO";
}
else
{
	echo "OK";
}



?>

  

 1 <body>
 2 
 3 <h1>添加数据</h1>
 4 <div>代号:<input type="text" id="ids" /></div>
 5 <div>名称:<input type="text" id="name" /></div>
 6 <input type="button" value="添加" id="add" />
 7 </body>
 8 <script type="text/javascript">
 9 $("#add").click(function(){
10     var ids = $("#ids").val();
11     var name = $("#name").val();
12     
13     $.ajax({
14         url:"tianjia2.php",
15         data:{i:ids,n:name},
16         type:"POST",
17         dataType:"TEXT",
18         success: function(data){
19             if(data.trim()=="OK")
20             alert("添加成功");
21             else
22             alert("添加失败");
23         }
24         
25     });
26 })
27 </script>
28 </html>
 1 <?php
 2 $ids = $_POST["i"];
 3 $name = $_POST["n"];
 4 require "DBDA.class.php";
 5 $db = new DBDA();
 6 
 7 $sql = "insert into fruit values('{$ids}','{$name}','','','','')";
 8 
 9 
10 if($db->query($sql,0))
11 {
12     echo "OK";
13 }
14 else
15 {
16     echo "NO";
17 }
18 ?>

 

posted @ 2017-05-12 15:17  张小琪  阅读(175)  评论(0编辑  收藏  举报