本篇文章主要介绍如何利用JqueryAjax+php制作简单注册登录页面,感兴趣的朋友参考下,希望对大家有所帮助。
HTML结构
<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>
前台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("注册成功") } } }) })
后台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;//注册成功 }
相关推荐:
Atas ialah kandungan terperinci 如何利用JqueryAjax+php制作简单注册登录页面. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!