PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

ajax php怎么实现注册

藏色散人
藏色散人 原创
2022-01-06 10:55:40 1872浏览

ajax php实现注册的方法:1、创建login.html注册登录页面;2、通过jquery代码实现验证码验证;3、使用php代码userlogin.php和adduser.php实现用户登录和注册功能即可。

本文操作环境:Windows7系统、PHP7.1版、Dell G3电脑。

ajax php怎么实现注册?

jQuery+ajax+php实现注册登录功能

html代码,login.html

<form>
	<label>用户名</label><input id="user-name" type="text" name="" />
	<label>密码</label><input id="user-password" type="password" name="" />
	<label>验证码</label><input id="code" type="text" name="" /><img src="php/getVerify.php" alt="" />
	<p class="clear"></p>
	<button type="button" id="login">登录</button>
	<button type="button" id="sign">注册</button>
</form>

jquery代码,login.js

$(&#39;img&#39;).click(function(){
		$(&#39;img&#39;).attr(&#39;src&#39;,&#39;php/getVerify.php?&#39;+Math.random());//刷新验证码
	})
	$(&#39;#login&#39;).click(function(){
		var username=$(&#39;#user-name&#39;).val();
		var password=$("#user-password").val();
		var code=$("#code").val();
		if(username!=""&&password!=""&&code.length==4){
			$.ajax({
				type:"POST",
				url:"php/userLogin.php",
				dataType:"JSON",
				data:{
					"user_name":username,
					"password":password,
					"code":code
				},
				success:function(data){
					switch(data){
						case 1://普通用户
							$.cookie("user",username);
							$.cookie("limit",0);
							window.location.href="index.php";
							break;
						case 2://管理员用户
							$.cookie("user",username);
							$.cookie("limit",1);
							window.location.href="index.php";
							break;
						case 3://密码错误
							alert("密码错误!");
							break;
						case 4://用户不存在
							alert("该用户不存在!");
							break;
						case 0://验证码错误
							alert("验证码不正确!");
							break;
					}
					
				}
			})
		}else{
			alert("请检查您的输入!");
		}
	})
	$(&#39;#sign&#39;).click(function(){
		var username=$(&#39;#user-name&#39;).val();
		var password=$("#user-password").val();
		var code=$("#code").val();
		if(username!=""&&password!=""&&code.length==4){
			$.ajax({
				type:"POST",
				url:"php/addUser.php",
				dataType:"JSON",
				data:{
					"user_name":username,
					"password":password,
					"code":code
				},
				success:function(data){
					switch(data){
						case 1://用户已存在
							alert("该用户已存在!请换一个用户名注册。")
							break;
						case 2://注册成功
							alert("注册成功!");
							$.cookie("user",username);
							$.cookie("limit",0);
							window.location.href="index.php";
							break;
						case 0://验证码错误
							alert("验证码不正确!");
							break;
					}
					
				}
			})
		}else{
			alert("请检查您的输入!");
		}
	})

php代码,userLogin.php

<?php
header("Content-type: text/html; charset=UTF-8");
session_start();
$name = $_POST[&#39;user_name&#39;];
$password=$_POST[&#39;password&#39;];
$code=$_POST[&#39;code&#39;];
$con=mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
if(!$con){
	die(&#39;error:&#39;.mysql_error());
}
mysql_select_db(&#39;db_name&#39;);
$result=mysql_query("select * from users where user_name=&#39;$name&#39;");
if($_SESSION[&#39;verify&#39;]==$code){
	if($row=mysql_fetch_array($result)){
		if($row[&#39;password&#39;]==$password){
			if($row[&#39;power&#39;]==0){
				echo 1;//普通用户
			}else{
				echo 2;//管理员用户
			}
		}else{
			echo 3;//密码错误
		}
	}else{
		echo 4;//用户不存在
	}
}else{
	echo 0;//验证码错误
}

addUser.php

<?php
header("Content-type: text/html; charset=UTF-8");
session_start();
$name = $_POST[&#39;user_name&#39;];
$password=$_POST[&#39;password&#39;];
$code=$_POST[&#39;code&#39;];
$con=mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
if(!$con){
    die(&#39;error:&#39;.mysql_error());
}
mysql_select_db(&#39;db_name&#39;);
$result=mysql_query("select * from users where user_name=&#39;$name&#39;");
if($_SESSION[&#39;verify&#39;]==$code){
    if($row=mysql_fetch_array($result)){
        echo 1;//用户已存在
    }else{//注册成功
        mysql_query("insert into `users` (`user_name`,`password`) values (&#39;$name&#39;,&#39;$password&#39;)");
        echo 2;
    }
}else{
    echo 0;
}

推荐学习:《PHP视频教程

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。