本文实例讲述了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 //入口文件
<?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 //客户操作页面,包含生成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>
<div id="resulte"><b>客户信息将在此处列出。</b></div>
</p>
6.get-ajax.phtml //最后根据由服务器取回的数据生成页面元素
<?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优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于zend framework框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!