tp架構之AJAX

來源:互聯網
上載者:User

標籤:名稱   cell   splay   extend   function   lock   block   1.0   window   

實現一個AJAX添加並顯示資料的例子

控制器

<?phpnamespace Home\Controller;use Think\Controller;class AjaxController extends Controller{    public function test()    {        $this->display();        }        public function xianshi()    {        $n = D("Nation");        $attr = $n->select();                $this->ajaxReturn($attr); //ajax返回方法            }        public function tianjia()    {        $this->display();        }        public function tianjiachuli()    {        $n = D("Nation");        $n->create();        $r = $n->add();        if($r)        {            $this->ajaxReturn("OK","eval");        }        else{            $this->ajaxReturn("NO","eval");        }    }}
View Code

顯示介面

<!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="__PUBLIC__/js/jquery-1.11.2.min.js"></script></head><body><table id="aa" width="100%" border="1" cellpadding="0" cellspacing="0">        </table><a href="__CONTROLLER__/tianjia">添加</a></body><script type="text/javascript">$(document).ready(function(e) {    $.ajax({        url:"__CONTROLLER__/xianshi",        data:{},        type:"POST",        dataType:"JSON",        success: function(data){            var aa = "<tr><td>代號</td><td>名稱</td><td>修改</td><td>刪除</td></tr>";            var str = "";            for(a in data)            {                str = str+"<tr><td>"+data[a].code+"</td><td>"+data[a].name+"</td><td><a href=‘__CONTROLLER__/xiugai/code/"+data[a].code+"‘>修改</a></td><td><a href=‘__CONTROLLER__/shanchu/code/"+data[a].code+"‘>刪除</a></td>";                }            $("#aa").html(aa+str);        }    })});</script></html>
View Code

添加頁面

<!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="__PUBLIC__/js/jquery-1.11.2.min.js"></script></head><body>        <div>代號:<input type="text" id="code" /></div><div>名稱:<input type="text" id="name" /></div><div><input type="button" id="btn"  value="添加" /></div><script type="text/javascript">$("#btn").click(function(){    var code = $("#code").val();    var name = $("#name").val();    $.ajax({        url:"__CONTROLLER__/tianjiachuli",        data:{Code:code,Name:name},        dataType:"TEXT",        type:"POST",        success:function(data){            if(data=="OK")            {                window.location.href="__CONTROLLER__/test";                }            else            {                alert("添加失敗");                }        }    });})    </script></body></html>
View Code

 

tp架構之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.