本篇文章主要介紹如何利用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;//注册成功 }
相關推薦:
以上是如何利用JqueryAjax+php製作簡單註冊登入頁面的詳細內容。更多資訊請關注PHP中文網其他相關文章!