首頁  >  文章  >  後端開發  >  Zend Framework框架中如何實作Ajax

Zend Framework框架中如何實作Ajax

小云云
小云云原創
2018-02-10 13:34:591231瀏覽

本文主要和大家介紹了Zend Framework框架中實現Ajax的方法,結合實例形式詳細分析了Zend Framework框架中實現ajax功能的具體步驟與相關操作技巧,希望能幫助到大家。

開發平台: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 //入口檔


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

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


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

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


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

4.TestController.php //控制器


<?php
  require_once &#39;Zend/Controller/Action.php&#39;;
  require_once &#39;Zend/View.php&#39;;
  require_once &#39;News.php&#39;;
  require_once &#39;TestDbCon.php&#39;;
  class TestController extends Zend_Controller_Action{
    public function ajaxAction(){
      $this->render();
    }
    public function getAjaxAction(){
//      $aaa=$_GET[&#39;q&#39;];
//      $this->view->sid=$_GET[&#39;sid&#39;];
      $aaa=$this->_request->getParam(&#39;q&#39;);
      $this->view->sid=$this->_request->getParam(&#39;sid&#39;);
      $conn=TestDbCon::getTestDbCon();
      $news_tb=new News(array(&#39;db&#39;=>$conn));
      $where=$news_tb->getAdapter()->quoteInto(&#39;title=?&#39;,$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>
<p id="resulte"><b>客户信息将在此处列出。</b></p>
</p>

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


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

相關推薦:

Zend Framework資料庫操作技巧總結

Zend Framework基於Command命令列建立ZF專案的方法

Zend Framework常用校驗器詳解

以上是Zend Framework框架中如何實作Ajax的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn