Zend Framework 프레임 워 크 에서 Ajax 를 실현 하 는 방법 예시

6449 단어 ZendFrameworkAjax
본 고 는 Zend Framework 프레임 워 크 에서 Ajax 를 실현 하 는 방법 을 실례 로 서술 하 였 다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
개발 플랫폼:Windows XP SP2
테스트 플랫폼:FreeBSD 7.0
개발 도구:Netbeans 6.1
사용 프레임 워 크:Zend Framework 1.5.2
데이터베이스:MySQL 5.0.51a
필요 한 데이터베이스 시트 와 ZF 관련 디 렉 터 리 및 파일:
1.시계:

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 |
+----+-------+---------------------+

2.디 렉 터 리:

3.관련 문서:
1.index.php//입구 파일
2.TestDbCon.phhp/데이터베이스 연결 파일
3.News.php/추상 화 된 데이터베이스 시트 파일
4.TestController.php//컨트롤 러
5.ajax.phtml//클 라 이언 트 작업 페이지,XML HttpRequest 생 성 대상,ajax 요청 보 내기,요청 처리,서버 반환 값 되 찾기 등 포함
6.get-ajax.pstml//마지막 으로 서버 에서 가 져 온 데이터 에 따라 페이지 요 소 를 생 성 합 니 다.
관련 파일 내용:
1.index.php//입구 파일

<?php
set_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//클 라 이언 트 작업 페이지,XML HttpRequest 생 성 대상,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.pstml//마지막 으로 서버 에서 가 져 온 데이터 에 따라 페이지 요 소 를 생 성 합 니 다.

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

더 많은 zend 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 는 Zend Framework 프레임 워 크 를 기반 으로 한 PHP 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기