Home >Backend Development >PHP Tutorial >How to use JqueryAjax+php to create a simple registration login page

How to use JqueryAjax+php to create a simple registration login page

墨辰丷
墨辰丷Original
2018-05-15 16:13:383281browse

This article mainly introduces how to use JqueryAjax php to create a simple registration login page. Interested friends can refer to it. I hope it will be helpful to everyone.

HTML structure


<p 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> 
			<p id="p"></p>
		</p>

Front-end 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){
							$("#p").html("密码正确")
							
						}else if(data==2){
							$("#p").html("密码不正确")
						}else if(data==3){
							$("#p").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){
							$("#p").html("用户存在不能注册")
						}else if(data==2){
							$("#p").html("注册成功")
						}
					}
					
				})
			})

Back-end login.php

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

Backend addUser.php

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

Related recommendations:

php mysql realizes simple login registration and password modification webpage

JS implements drop-down menu login registration pop-up window

Login registration box mobile phone number and verification code verification implementation code

The above is the detailed content of How to use JqueryAjax+php to create a simple registration login page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn