您的位置:HBcms宏博内容管理系统 PHP技术 正文
原作者:hbcms 添加时间:2007-05-26 原文发表:2007-05-26 人气:602

本文章共6540字,分2页,当前第1页,快速翻页:
 

PHP的PEAR::HTML_AJAX类库应用

HTML_AJAX下载http://pear.php.net/package/HTML_AJAX/download

Pear:HTML_AJAX是一个相当成熟的Ajax框架, 使用JSON进行数据传输。內建丰富的例子,包括留言板、登录、grab…等等。

简单的例子:

Setting up a server

You can use HTML_AJAX in many different ways but most setups use an instance of HTML_AJAX_Server to deliver both the javascript libraries and to handle AJAX requests from browsers.

A basic server is shown below, in the file server.php:


<?php
//a session is required(you can also set session.auto_start=1 in php.ini)
session_start();

include 'HTML/AJAX/Server.php';

$server = new HTML_AJAX_Server();
$server->handleRequest();
?>

This basic setup gives you access to the HTML_AJAX javascript libraries, which will allow you to do basic AJAX actions like replacing the content of a div with the output of a url on your server.

Let us now create a new webpage with a div container and a button. Whenever we push the button we want to fetch some data and update the div container. Save the file as page.html:

<html>

<script type="text/javascript" src="server.php?client=all"></script>
<div id="target">I'm the target</div>
<script type="text/javascript">
HTML_AJAX.replace('target','output.php');
</script>
   <form>
      <input type="button" onclick="HTML_AJAX.replace('target','output.php');" value="Update target">
   </form>
</html>

This will load the content of output.php into the <div> with an ID of "target" when HTML_AJAX.replace() is called (i.e. when you push the button).

Now let's create the file that generates the dynamic stuff. It could be anything, like fetching stuff from a database or whatever. For now we are going to get the local time, and we do it in the file output.php:


<?php

   print("<h1>". strftime("%H:%M:%S") ."</h1>");
?>

There! Each time you press Update target, the target is replaced with the current time. Now it's up to your imagination to use this to something more useful.


 
本页地址

相关文章

PHP6下载,php6介绍和php6安装手册
Smarty+PEAR::Pager+PEAR::HTML_table列表
PHP ASP比较:PHP比ASP优秀的七个理由
php ajax实例: 开发高质量的RSS聚合器
PHP程序员易忽略的PHP精华和技巧
用php实现远程安装:自动下载,自动解压
可能是PEAR:config组件的bug
PHP官方网站,php官方论坛,中文函数手册下
最佳的PHP数据库MySQL市场主管采访
SMTP判断邮箱是否存在,检查email地址是否真
php程序员前途,mysql数据库和oracle的区别
PHP生成静态页面详解
用PHP实现验证码功能
在线操作系统----TOMOS的未来战略(作者:谢
[转]PHP突出开源优势要做Windows平台一等公
用PHP与XML进行网站编程

相关评论


本文章所属分类:首页 PHP技术