try another color:
try another fontsize: 70% 80% 90% 100%
Zend Framework 中文站

在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 //入口文件

   1. <?php  
   2.   
   3. set_include_path('.' . PATH_SEPARATOR .'../library' . PATH_SEPARATOR . get_include_path() . PATH_SEPARATOR . '../application/modules/default/models' . PATH_SEPARATOR . '../application/modules/admin/models');  
   4.   
   5. require_once 'Zend/Controller/Front.php';  
   6.   
   7. require_once 'Zend/Controller/Router/Route.php';  
   8.   
   9.   
  10.   
  11. $ctrl=Zend_Controller_Front::getInstance();  
  12.   
  13. $ctrl->addModuleDirectory('../application/modules');  
  14.   
  15. $ctrl->throwExceptions(true);  
  16.   
  17. $ctrl->dispatch();  
  18.   
  19. ?>   

2.TestDbCon.phhp //数据库连接文件
   1. <?php  
   2.   
   3.     require_once 'Zend/Db.php';  
   4.   
   5.     require_once 'Zend/Registry.php';  
   6.   
   7.   
   8.   
   9.     class  TestDbCon{  
  10.   
  11.         public static function getTestDbCon(){  
  12.   
  13.             $params=array(  
  14.   
  15.             'host'=>'localhost',  
  16.   
  17.             'username'=>'root',  
  18.   
  19.             'password'=>'123456',  
  20.   
  21.             'dbname'=>'test'  
  22.   
  23.             );  
  24.   
  25.             $con=Zend_Db::factory('Pdo_Mysql',$params);  
  26.   
  27.             return $con;  
  28.   
  29.         }  
  30.   
  31.     }    
  32.   
  33. ?>  

3.News.php //抽象出来的数据库表文件
   1. <?php  
   2.   
   3. /** 
   4.  
   5.  * PHP Template. 
   6.  
   7.  */  
   8.   
   9. require_once 'Zend/Db/Table/Abstract.php';  
  10.   
  11.   
  12.   
  13.   
  14.   
  15. class News extends Zend_Db_Table_Abstract{     
  16.   
  17.    //    protected $_schema='test';  
  18.   
  19.        protected $_name='news';  
  20.   
  21.        protected $_primary='id';  
  22.   
  23.        protected $_sequence=true;  
  24.   
  25. }  
  26.   
  27. ?>  

4.TestController.php //控制器
   1. <?php  
   2.   
   3.     require_once 'Zend/Controller/Action.php';  
   4.   
   5.     require_once 'Zend/View.php';  
   6.   
   7.     require_once 'News.php';  
   8.   
   9.     require_once 'TestDbCon.php';  
  10.   
  11.   
  12.   
  13.     class TestController extends Zend_Controller_Action{  
  14.   
  15.         public function ajaxAction(){  
  16.   
  17.             $this->render();  
  18.   
  19.         }  
  20.   
  21.         public function getAjaxAction(){  
  22.   
  23. //            $aaa=$_GET['q'];  
  24.   
  25. //            $this->view->sid=$_GET['sid'];  
  26.   
  27.             $aaa=$this->_request->getParam('q');  
  28.   
  29.             $this->view->sid=$this->_request->getParam('sid');  
  30.   
  31.               
  32.   
  33.             $conn=TestDbCon::getTestDbCon();  
  34.   
  35.             $news_tb=new News(array('db'=>$conn));  
  36.   
  37.             $where=$news_tb->getAdapter()->quoteInto('title=?',$aaa);  
  38.   
  39.             $this->view->rowSet=$news_tb->fetchAll($where);  
  40.   
  41.             $this->render();  
  42.   
  43.         }  
  44.   
  45.     }  
  46.   
  47. ?>  

5.ajax.phtml //客户操作页面,包含生成XMLHttpRequest对象,发ajax请求,处理请求,取回服务器返回值等
   1. <script type="text/javascript">  
   2.   
   3.     var xmlHttp  
   4.   
   5.     function showValue(str)  
   6.   
   7.     {   
   8.   
   9.         xmlHttp=getXmlHttpObject();  
  10.   
  11.         if (xmlHttp==null)  
  12.   
  13.         {  
  14.   
  15.             alert ("您的浏览器不支持AJAX.");  
  16.   
  17.             return;  
  18.   
  19.         }   
  20.   
  21.         var url="/test/get-ajax";  
  22.   
  23.         url=url+"/q/"+str;  
  24.   
  25.         url=url+"/sid/"+Math.random();  
  26.   
  27.         xmlHttp.onreadystatechange=stateChanged;  
  28.   
  29.         xmlHttp.open("GET",url,true);  
  30.   
  31.         xmlHttp.send(null);  
  32.   
  33.     }  
  34.   
  35.   
  36.   
  37.     function stateChanged()  
  38.   
  39.     {   
  40.   
  41.         if (xmlHttp.readyState==4)  
  42.   
  43.         {   
  44.   
  45.             document.getElementById("resulte").innerHTML=xmlHttp.responseText;  
  46.   
  47.         }  
  48.   
  49.     }  
  50.   
  51.   
  52.   
  53.     function getXmlHttpObject()  
  54.   
  55.     {  
  56.   
  57.         var xmlHttp=null;  
  58.   
  59.         try  
  60.   
  61.         {  
  62.   
  63.             // Firefox, Opera 8.0+, Safari  
  64.   
  65.             xmlHttp=new XMLHttpRequest();  
  66.   
  67.         }  
  68.   
  69.         catch (e)  
  70.   
  71.         {  
  72.   
  73.             // Internet Explorer  
  74.   
  75.             try  
  76.   
  77.             {  
  78.   
  79.                 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  
  80.   
  81.             }  
  82.   
  83.             catch (e)  
  84.   
  85.             {  
  86.   
  87.                 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");  
  88.   
  89.             }  
  90.   
  91.         }  
  92.   
  93.         return xmlHttp;  
  94.   
  95.     }  
  96.   
  97. </script>  
  98.   
  99.   
 100.   
 101. <form>  
 102.   
 103.     请选择一位客户:  
 104.   
 105.     <select name="customers" onchange="showValue(this.value)">  
 106.   
 107.     <option value="rot">rot</option>  
 108.   
 109.     <option value="aaa">aaa</option>  
 110.   
 111.     <option value="jesse">jesse</option>  
 112.   
 113.     <option value="andle">andle</option>  
 114.   
 115.     </select>  
 116.   
 117. </form>  
 118.   
 119.   
 120.   
 121. <p>  
 122.   
 123. <div id="resulte"><b>客户信息将在此处列出。</b></div>  
 124.   
 125. </p>  

6.get-ajax.phtml //最后根据由服务器取回的数据生成页面元素
   1. <?php  
   2.   
   3.     foreach($this->rowSet as $row){  
   4.   
   5.         echo "<div>";  
   6.   
   7.         echo "<ul>";  
   8.   
   9.         echo "<li>";  
  10.   
  11.         echo "id=".$row->id." title=".$row->title." add_time=".$row->add_time;  
  12.   
  13.         echo "</li>";  
  14.   
  15.         echo "</ul>";  
  16.   
  17.         echo "</div>";  
  18.   
  19.         
  20.   
  21.     }  
  22.   
  23.   echo $this->sid;  
  24.   
  25. ?>