PHP头条
热点:

JqueryAjax+php实现简单的注册登录,jqueryajaxphp


HTML结构

<div class="container">
			<form>  
			    <label>用户名</label>
			    	<input id="username" type="text" name="username" class="form-control" />  
			    <label>密码</label>
			    	<input id="password" type="password" name="password" class="form-control" />  
			    <button type="button" id="login" class="btn btn-primary">登录</button>  
			    <button type="button" id="sign" class="btn btn-danger">注册</button>  
			</form> 
			<div id="div"></div>
		</div>
前台JS

$("#login").click(function(){
				var sendData = {"username":$("#username").val(),"password":$("#password").val()}
				$.ajax({
					url:"action/login.php",
					type:"POST",
					data:sendData,
					success:function(data){
						if(data==1){
							$("#div").html("密码正确")
							
						}else if(data==2){
							$("#div").html("密码不正确")
						}else if(data==3){
							$("#div").html("账号不存在")
						}
					}
					
				})
			})
			$("#sign").click(function(){
				var sendData = {"username":$("#username").val(),"password":$("#password").val()}
				$.ajax({
					url:"action/addUser.php",
					type:"POST",
					data:sendData,
					success:function(data){
						if(data==1){
							$("#div").html("用户存在不能注册")
						}else if(data==2){
							$("#div").html("注册成功")
						}
					}
					
				})
			})
后台login.php

	$username = $_POST['username'];
	$password = $_POST['password'];
	$conn = mysqli_connect("localhost","root","","login") or die("连接失败");
	mysqli_query($conn,"set names utf8");
	$result = mysqli_query($conn,"select * from user where username='$username'");
	
	if($row=mysqli_fetch_array($result)){
		if($row["password"]==$password){
			echo 1;//密码正确
		}else{
			echo 2;//密码不正确
		}
	}else{
		echo 3;//账号不正确
	}
后台addUser.php

	$username = $_POST['username'];
	$password = $_POST['password'];
	$conn = mysqli_connect("localhost","root","","login") or die("连接失败");
	mysqli_query($conn,"set names utf8");
	$result = mysqli_query($conn,"select * from user where username='$username'");
	if($row=mysqli_fetch_array($result)){
		echo 1;//"用户存在不能注册"
	}else{
		mysqli_query($conn,"insert into `user` (`username`,`password`) values ('$username','$password')");
		echo 2;//注册成功
	}

希望能对新手玩家有点帮助。


www.phpzy.comtrue/php/3528.htmlTechArticleJqueryAjax+php实现简单的注册登录,jqueryajaxphp HTML结构 div class="container"form label用户名/label input id="username" type="text" name="username" class="form-control" / label密码/label input id="password" type="password"...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐