>  기사  >  백엔드 개발  >  Zend Framework에서 Ajax를 구현하는 방법

Zend Framework에서 Ajax를 구현하는 방법

小云云
小云云원래의
2018-02-10 13:34:591232검색

이 글은 주로 Zend Framework 프레임워크에서 Ajax를 구현하는 방법을 소개하며, Zend Framework 프레임워크에서 Ajax 기능을 구현하는 구체적인 단계와 관련 운영 기법을 예제 형식으로 자세히 분석하는 데 도움이 되기를 바랍니다. 너.

개발 플랫폼: Windows



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. 디렉터리:


관련 파일:

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 //Controller


<?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 Line Project 방식을 기반으로 한 ZF

Zend Framework에서 흔히 사용되는 검증인에 대한 자세한 설명

위 내용은 Zend Framework에서 Ajax를 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.