This article mainly describes how to make use of jqueryajax+php to create a simple registration login page, interested in the friend's reference, I hope to help you.
HTML structure
<p class= "Container" ><form> <label> user name </label> <input id= "username" type= "text "Name=" username "class=" Form-control "/> <label> password </label> <input id=" password "type=" Password "name=" password "class=" Form-control "/> <button type=" button "id=" Login "class=" btn btn-primary " > Login </button> <button type= "button" id= "sign" class= "BTN Btn-danger" > Registration </button> </ form> <p id= "P" ></p></p>
Front desk 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 ("password correct") }else if (data==2) {$ ("#p"). HTML ("password incorrect")}else if (data==3) {$ ("#p"). HTML ("account does not Exist")}})}) $ ("#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 ("User exists cannot register")}else if (data==2) {$ ("#p"). HTML ("Registration succeeded")}})})
Backstage login.php
$username = $_post[' username '); $password = $_post[' password '); $conn = Mysqli_connect ("localhost", "root", "", "Login") Or Die ("Connection Failed"); 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;//password is correct}else{echo 2;// The password is incorrect}}else{echo 3;//account is incorrect}
Backstage adduser.php
$username = $_post[' username '); $password = $_post[' password '); $conn = Mysqli_connect ("localhost", "root", "", "Login") Or Die ("Connection Failed"); Mysqli_query ($conn, "Set names UTF8"); $result = Mysqli_query ($conn, "select * from user where username= ' $ Username ' "), if ($row =mysqli_fetch_array ($result)) {echo 1;//" user exists cannot register "}else{mysqli_query ($conn," insert INTO ' "User" ( ' username ', ' password ') VALUES (' $username ', ' $password ') "); Echo 2;//Registration Successful}