1. 程式人生 > >ajax註冊

ajax註冊

document -type pla 沒有 text echo htm ont utf-8

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"用戶名可以使用";
}

?>

ajax註冊