Implement Ajax in Zend framework

Source: Internet
Author: User
Tags php template zend framework

Development Platform: Windows XP SP2
Test Platform: FreeBSD 7.0
Development tools: netbeans 6.1
Framework: Zend framework 1.5.2
Database: MySQL 5.0.51a

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

Required database tables and ZF related directories and files:

I. Table:

Mysql> select * from news;

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

| ID | title | add_time

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

| 22 | rot | 00:00:00 |

| 23 | AAA | 00:00:00 |

| 24 | rot | 00:00:00 |

| 29 | dfeew | 00:00:00 |

| 26 | Jesse | 00:00:00 |

| 27 | Andle | 00:00:00 |

| 28 | Andle | 00:00:00 |

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

Ii. directory:

Iii. Related documents:

1. index. php // entry file

2. testdbcon. phhp // database connection file

3. News. php // abstracted database table File

4. testcontroller. php // Controller

5. Ajax. phtml // client operation page, including generating XMLHttpRequest objects, sending Ajax requests, processing requests, and retrieving server return values

6. get-ajax.phtml // finally generate page elements based on the data retrieved by the server

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

Related 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 // abstracted 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 operation page, including generating XMLHttpRequest objects, sending Ajax requests, processing requests, and retrieving server return values

<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> select a customer: <select name =" MERs "onchange =" showvalu E (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> the customer information is stored in. </B> </div> </P>

6. get-ajax.phtml // finally generate page elements based on the data retrieved by the server

<?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;?>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.