在Zend Framework 中實現Ajax

來源:互聯網
上載者:User

開發平台:Windows XP SP2
測試平台:FreeBSD 7.0
開發工具:Netbeans 6.1
使用架構:Zend Framework 1.5.2
資料庫: MySQL 5.0.51a

**************************************** 

所需的資料庫表和ZF相關目錄以及檔案:

 一、表:

mysql> select * from news;

+----+-------+---------------------+

| id | title | add_time           

| +----+-------+---------------------+

| 22 | rot   | 2008-01-04 00:00:00 |

| 23 | aaa   | 2008-01-04 00:00:00 |

| 24 | rot   | 2008-01-04 00:00:00 |

| 29 | dfeew | 2008-02-27 00:00:00 |

| 26 | jesse | 2008-02-27 00:00:00 |

| 27 | andle | 2008-02-27 00:00:00 |

| 28 | andle | 2008-02-27 00:00:00 |

+----+-------+---------------------+

二、目錄:

三、相關檔案:

1.index.php //入口檔案

2.TestDbCon.phhp //資料庫連接檔案

3.News.php //抽象出來的資料庫表檔案

4.TestController.php //控制器

5.ajax.phtml //客戶操作頁面,包含產生XMLHttpRequest對象,發ajax請求,處理請求,取回伺服器傳回值等

6.get-ajax.phtml //最後根據由伺服器取回的資料產生頁面元素

**********************************************

相關檔案內容:

1.index.php //入口檔案

<?phpset_include_path('.' . PATH_SEPARATOR .'../library' . PATH_SEPARATOR . get_include_path() . PATH_SEPARATOR . '../application/modules/default/models' . PATH_SEPARATOR . '../application/modules/admin/models');require_once 'Zend/Controller/Front.php';require_once 'Zend/Controller/Router/Route.php';$ctrl=Zend_Controller_Front::getInstance();$ctrl->addModuleDirectory('../application/modules');$ctrl->throwExceptions(true);$ctrl->dispatch();?> 

2.TestDbCon.phhp //資料庫連接檔案

<?php    require_once 'Zend/Db.php';    require_once 'Zend/Registry.php';    class  TestDbCon{        public static function getTestDbCon(){            $params=array(            'host'=>'localhost',            'username'=>'root',            'password'=>'123456',            'dbname'=>'test'            );            $con=Zend_Db::factory('Pdo_Mysql',$params);            return $con;        }    }  ?>

 

3.News.php //抽象出來的資料庫表檔案

<?php/** * PHP Template. */require_once 'Zend/Db/Table/Abstract.php';class News extends Zend_Db_Table_Abstract{      //    protected $_schema='test';       protected $_name='news';       protected $_primary='id';       protected $_sequence=true;}?>

4.TestController.php //控制器

<?php    require_once 'Zend/Controller/Action.php';    require_once 'Zend/View.php';    require_once 'News.php';    require_once 'TestDbCon.php';    class TestController extends Zend_Controller_Action{        public function ajaxAction(){            $this->render();        }        public function getAjaxAction(){//            $aaa=$_GET['q'];//            $this->view->sid=$_GET['sid'];            $aaa=$this->_request->getParam('q');            $this->view->sid=$this->_request->getParam('sid');                        $conn=TestDbCon::getTestDbCon();            $news_tb=new News(array('db'=>$conn));            $where=$news_tb->getAdapter()->quoteInto('title=?',$aaa);            $this->view->rowSet=$news_tb->fetchAll($where);            $this->render();        }    }?>

 

5.ajax.phtml //客戶操作頁面,包含產生XMLHttpRequest對象,發ajax請求,處理請求,取回伺服器傳回值等 

<script type="text/javascript">    var xmlHttp    function showValue(str)    {         xmlHttp=getXmlHttpObject();        if (xmlHttp==null)        {            alert ("您的瀏覽器不支援AJAX.");            return;        }         var url="/test/get-ajax";        url=url+"/q/"+str;        url=url+"/sid/"+Math.random();        xmlHttp.onreadystatechange=stateChanged;        xmlHttp.open("GET",url,true);        xmlHttp.send(null);    }    function stateChanged()    {         if (xmlHttp.readyState==4)        {             document.getElementById("resulte").innerHTML=xmlHttp.responseText;        }    }    function getXmlHttpObject()    {        var xmlHttp=null;        try        {            // Firefox, Opera 8.0+, Safari            xmlHttp=new XMLHttpRequest();        }        catch (e)        {            // Internet Explorer            try            {                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");            }            catch (e)            {                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");            }        }        return xmlHttp;    }</script><form>    請選擇一位客戶:    <select name="customers" onchange="showValue(this.value)">    <option value="rot">rot</option>    <option value="aaa">aaa</option>    <option value="jesse">jesse</option>    <option value="andle">andle</option>    </select></form><p><div id="resulte"><b>客戶資訊將在此處列出。</b></div></p>

6.get-ajax.phtml //最後根據由伺服器取回的資料產生頁面元素

<?php    foreach($this->rowSet as $row){        echo "<div>";        echo "<ul>";        echo "<li>";        echo "id=".$row->id." title=".$row->title." add_time=".$row->add_time;        echo "</li>";        echo "</ul>";        echo "</div>";          }  echo $this->sid;?>
相關文章

聯繫我們

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