tthinkphp對於ajax的調用及session的用法

來源:互聯網
上載者:User

標籤:pac   html   inpu   類構造   複製   content   ace   實用   color   

一、ajax對於php是非常實用的一種方法,那麼在thinkphp架構中ajax是不是一樣好用呢?答案是肯定的。thinkphp針對ajax也有自己獨特的用法:

首先我們在自己建的Admin檔案下建立一個Index檔案夾,然後在這個檔案夾裡邊建一個ceshi.html檔案,要用ajax就要用到jquery檔案,所以我們要將jquery檔案複製到Public檔案夾下進行調用:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><script src="__ROOT__/Public/js/jquery-3.2.0.min.js">//調用jquery</script></head><body>    <select id ="leibie">    </select></body><script>    $.ajax({        url:"__CONTROLLER__/shuju",        dataType:"JSON",        success:function(data){            var str = "";            for( var k in data){                str = str+"<option value=‘"+data[k].lbid+"‘>"+data[k].lbname+"</option>";            }            $("#leibie").html(str);        }    })</script></html>

然後我們在Controller檔案夾內建立一個IndexController.class.php的檔案,在裡邊添加ceshi方法和shuju方法:

<?phpnamespace Admin\Controller;use Think\Controller;class IndexController extends Controller{    public function index(){        echo "歡迎使用Thinkphp";    }        public function ceshi(){            $this->show();        }        public function shuju(){                $n = M("leibie");//建立leibie表模型                $arr = $n->select();                $this->ajaxReturn($arr);//調該方法返回資料        }    }

看一下結果:

這就是thinkphp中ajax的用法,非常方便實用。

二、session在thinkphp中的使用,用於驗證使用者是否登入:

在thinkphp中session不用手動開啟,預設就是開啟的:

首先在Cotroller檔案夾內建一個LoginController.class.php

<?phpnamespace Home\Controller;use Think\Controller;class LoginController extends Controller{    public function login(){        if(empty($_POST)){            $this->show();        }        else{            $uid = $_POST["uid"];            $pwd = $_POST["pwd"];            $n=M("users");            $arr = $n->find($uid);            if($arr["pwd"]==$pwd && !empty($pwd)){//驗證密碼                session("uid","$uid");            }            else{                echo "登入失敗";            }        }    }}

然後在View檔案夾下建立一個login.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><script src="__ROOT__/Public/js/jquery-3.2.0.min.js"></script></head><body>  <h1>登入頁面</h1>    <form action="__ACTION__" method="post">        <div>使用者名稱:<input type="text" name="uid"></div>        <div>密碼:<input type="password" name="pwd"></div>        <input type="submit" value="登入">    </form></body></html>

 然後最關鍵的是需要建立一個父類BaseController.class.php:

<?phpnamespace Home\Controller;use Think\Controller;class BaseController extends Controller{    public function __construct(){        //調用父類構造        parent::__construct();        //寫session控制        if(session("?uid")){        }else{            $this->redirect("Login/login");        }    }}

然後我們其他所有的控制器都繼承建立的這個BaseController就可以了:

<?phpnamespace Admin\Controller;use Home\Controller\BaseController;class IndexController extends BaseController{    public function index(){        echo "歡迎使用Thinkphp";    }        public function ceshi(){            $this->show();        }        public function shuju(){                $n = M("leibie");//建立leibie表模型                $arr = $n->select();                $this->ajaxReturn($arr);//調該方法返回資料,如果是字串,後邊加eval        }            }

 

tthinkphp對於ajax的調用及session的用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.