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

墨辰丷
Release: 2023-03-25 20:42:02
Original
3171 people have browsed it

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>
Copy after login

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("注册成功")
						}
					}
					
				})
			})
Copy after login

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;//账号不正确
	}
Copy after login

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;//注册成功
	}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template