用PHP和Ajax進行前後台資料互動——以使用者登入狀態例

來源:互聯網
上載者:User

標籤:.com   exp   bootstrap   txt   post   form   流程   data   我不知道   

很多網站中都有使用者登入系統,要完成使用者的註冊和登陸,就一定要用到前背景資料互動。在這裡以簡單的使用者註冊和登陸為例介紹一下前後台互動的大致流程。

 

首先,我們來做一個簡單的登陸介面。

這裡為了方便我使用了bootstrap外掛程式

<form class="form-horizontal">    <div class="form-group">        <label>使用者名稱</label>        <input type="text" class="form-control" name="userName"/>    </div>    <div class="form-group">        <label>密碼</label>        <input type="password" class="form-control" name="pwd"/>    </div>    <div class="form-group btns">        <input type="button" class="btn btn-primary" value="登入系統" id="submit"/>        &nbsp;&nbsp;&nbsp;&nbsp;        <a type="button" class="btn btn-success" href="reg.php"/>註冊帳號</a>    </div></form>

 

 先做一個註冊的功能,點擊註冊帳號跳轉到註冊的頁面。註冊的頁面跟登入頁面類似,點擊返回登陸能回到登入頁。

 給幾個input添加name

<input type="text" class="form-control" name="userName"/><input type="password" class="form-control" name="pwd" /><input type="password" class="form-control" name="rePwd" /><input type="button" class="btn btn-primary" value="確定註冊" id="submit"/>

 

接下來開始寫JS代碼,點擊註冊按鈕,觸發click事件。

1、我們把form表單傳遞的資訊通過serialize()將轉為字串。

2、通過post()將資料提交給幕後處理,第一個參數為提交的檔案,第二個參數是傳遞的資料,這裡我們寫為對象的形式。

$(function(){    $("#submit").on("click",function(){        var str = $("form").serialize();        $.post("doReg.php",{"formData":str},function(data){            if(data=="true"){                alert("註冊成功!即將跳轉登陸頁!");                location = "login.php";            }else{                alert("註冊失敗!因為啥我不知道!");            }        });    });});

 

然後我們轉到後台指令碼

 1、將接收的資料以"[;]"分隔,便於我們到時候分隔字串。

$str = $_POST["formData"]."[;]";

2、這裡我們先將資料寫入一個txt檔案,返回新增欄位的長度,後期可以將資料儲存到資料庫。

$num = file_put_contents("user.txt", $str,FILE_APPEND);

3、 然後給前台發送一個資料,當$num>0說明檔案寫入了內容。

if($num>0){    echo "true";}else{    echo "false";}

 

這樣我們就完成了一個簡單的前後台資料互動。

 

完成註冊以後我們可以回到登陸頁面。

登陸頁面的JS代碼也是類似的

$(function(){    $("#submit").on("click",function(){        var str = $("form").serialize();        $.post("doLogin.php",{"formData":str},function(data){            if(data=="true"){                location = "index.php?name="+$("input[name=‘userName‘]").val();            }else{                alert("使用者名稱或密碼錯誤!!!");            }        });    });});

 

接下來我們來進行後台操作

1、首先取到前台發來的資料

$str = $_POST["formData"];

2、然後將接收到的資料按照"&"符號進行分隔,因為我們存入的資料都是中間以&分隔,最後以[;]結尾

list()的用法是可以將分隔之後的各段的字串分別賦予不同的變數。

list($userName) = explode("&", $str);list(,$pwd) = explode("&", $str);

3、然後我們要取到存資料的檔案

$users = file_get_contents("user.txt");

4、然後將每個使用者區分,這次我們用[;]分隔

$userArr = explode("[;]", $users);

5、然後輸入的登陸資訊是不是跟我們檔案裡註冊的使用者資訊一致,一致說明輸入的資訊正確,就可以結束指令碼並給前台傳回true

foreach ($userArr as $user) {    list($realName) = explode("&", $user);    list(,$realPwd) = explode("&", $user);    if($userName==$realName&&$pwd==$realPwd){        echo "true";        die();    }}
echo "false";

 

然後在前台判斷如果傳回的是true就可以跳轉頁面了。

 

用PHP和Ajax進行前後台資料互動——以使用者登入狀態例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.