ajax注册 查询一下数据库中有时没有这个name有就被注册没有就可以使用

HTML代码:

 

<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="js/jquery.js"></script>

</head>
<script>
$(document).ready(function(){
$("#name").focus();
$("#name").blur(function(){
var a=$("#name").val();

if (a) {
$.post("index1ajax2.php",{"name":a},function (data){
alert(data); //查询一下数据库中有时没有这个name有就被注册没有就可以使用
});

}else{

alert("用户命不能为空");
};

 

 


});
//实现注册功能
$("#bu1").click(function(){
var a=$("#name").val();
var b=$("#mima").val();
var c=$("#xingbie").val(); //val()是返回属性value对应的值
var d=$("#xibu").val();
var e=$("#banji").val();
var f=$("#tex").val();

$.post("index1.php",{"name":a,"mima":b,"xingbie":c,"xibu":d,"banji":e,"tex":f},function (data1){
alert(data1); //弹窗

}); //index1.php引入php文件运行这个文件 {}中间写上面var a=$("#name").val();对应的值 funtion定义个变量用alert弹窗输出

});

});

 

 

</script>


<body>
<input type="text" id="name" placeholder="姓名" required="">
<input type="password" id="mima" required=" " placeholder="密码至少6位数"><p></p>
<input type="text" id="xingbie" placeholder="性别" required="">
<input type="text" id="xibu" placeholder="系部" required="">
<input type="text" id="banji" placeholder="班级" required="">
<input type="text" id="tex" placeholder="电话" required="">
<button type="button" id="bu1">确定</button>

  

</body>

 

</body>
</html>

 

 

index1.php代码

 

<?php


header( 'Content-Type:text/html;charset=utf-8 ');


include_once("conn/conn.php"); //引入导入数据库控制页面

$name=$_POST["name"];
$mima=$_POST["mima"];
$xingbie=$_POST["xingbie"]; // 括号里面的值对应前端里面name
$xibu=$_POST["xibu"];
$banji=$_POST["banji"];
$tex=$_POST["tex"];

$sql="INSERT into user (name,mima,xingbie,xibu,banji,tex)values('$name','$mima','$xingbie','$xibu','$banji','$tex')"; //规定查询字符串
$r=mysqli_query($link,$sql);// $link要使用的mysql连接 $sql要查询的字符串
if ($r) {
echo ("写入成功");
}else{
echo ("失败");
}

 

 

?>

index1ajax2.php代码

 

 

<?php


header( 'Content-Type:text/html;charset=utf-8 ');


include_once("conn/conn.php"); //引入导入数据库控制页面

$name=$_POST["name"];

 

$sql="SELECT*from user where name ='$name' ";
$r=mysqli_query($link,$sql);// $link要使用的mysql连接 $sql要查询的字符串
$n=mysqli_num_rows($r);
if ($n>0) {
echo "用户名被注册";
}else{
echo"用户名可以使用";
}

 

 

?>

 

posted @ 2017-11-23 15:44  旧友丶  阅读(130)  评论(0编辑  收藏  举报