In this paper, we introduce the method of implementing Ajax in Zend Framework framework, and analyze the specific steps and related operation skills of Ajax function in Zend Framework frame in detail, and hope to help everyone.
Development platform: Windows XP SP2
Test Platform: FreeBSD 7.0
Development tools: Netbeans 6.1
Working with Frames: Zend Framework 1.5.2
Database: MySQL 5.0.51a
Required database tables and ZF-related directories and files:
One, table:
Mysql> SELECT * from news;+----+-------+---------------------+| ID | Title | add_time| +----+-------+---------------------+| 22 | Rot | 2008-01-04 00:00:00 | | | | AAA | 2008-01-04 00:00:00 | | | Rot | 2008-01-04 00:00:00 | | | | 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 |+----+-------+---------------------+
Second, directory:
Iii. Related documents:
1.index.php//Entry file
2.TESTDBCON.PHHP//Database connection file
3.news.php//Abstract database table file
4.testcontroller.php//Controller
5.ajax.phtml//Client Operations page, including generating XMLHttpRequest objects, AJAX requests, processing requests, retrieving server return values, etc.
6.get-ajax.phtml//Finally generate page elements based on data retrieved by the server
Relevant file content:
1.index.php//Entry file
<?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//Database connection file
<?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//Abstract database table file
<?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//Controller
<?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//Client Operations page, including generating XMLHttpRequest objects, AJAX requests, processing requests, retrieving server return values, etc.
<script type= "Text/javascript" > var xmlHttp function showvalue (str) {xmlhttp=getxmlhttpobject (); if (xmlhttp==null) {alert ("Your browser does not support 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> Please select a client: <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> customer information will be listed here. </b></p></p>
6.get-ajax.phtml//Finally generate page elements based on data retrieved by the server
<?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;? >