Use PHPRPC to implement Ajax cascading drop-down menu_PHP tutorial

WBOY
Release: 2016-07-13 17:33:27
Original
685 people have browsed it

级联下拉菜单就是从一个下拉菜单中选中一项后,相应的另一个下拉菜单的内容会随之改变。

一般来说,最简单的,就是每次选中都提交一次表单,刷新整个页面。这也是用户体验度最差的。

另一种是把所有选项在第一次加载时就全部载入整个页面中的 JavaScript 数组中,然后级联通过 JavaScript 来控制,在整个数据量不大时,这是一个不错的实现无刷新并且快速的方法,但是当整个数据量非常大时,这种方法就会使第一次加载变得非常慢了。

还有就是采用 ajax(动态网站静态化) 方式,即开始只载入第一层菜单的内容,当用户选中第一层菜单的某项时,再通过 xml(标准化越来越近了)HttpRequest 来获取相应选项所对应的第二层菜单的内容。这种方式效果最好,但是采用传统方式来编写这样的 ajax(动态网站静态化) 程序代码量会比较多。而且如果设计不好,服务器端返回菜单内容的程序的可复用性也会很差。

但是在本文中你会看到用 php(做为现在的主流开发语言)RPC 来实现这种 ajax(动态网站静态化) 效果是多么的简单,并且还会具有非常高的可复用性。


本文以省市两级级联下拉菜单为例,为了举例方便,本文中采用的是 SQLite 数据库,因为这个文件型数据库比较容易部署,而且查询效率很高(当然创建该数据库的效率不高,但创建仅一次而已,该数据库在该程序中内容是不变的),不过服务器需要安装 SQLite 扩展。

这个数据库中的表只有 2 个,一个 province 表,一个 city 表。province 表中,只有 id 和 name 两个字段,分别是省份编号(主键)和省名。city 表中,有 id、name 和 pid 三个字段,id 是城市编号,name 是城市名,pid 是城市所在省的编号,与 province 表中的省份编号相对应。

创建该数据库的程序这里就不给出来了,它包含在后面提供的实例下载中。

下面来看看创建这个程序的服务器端有多么简单,为了提高可复用性,我们把服务器端分为了 2 个文件,一个是 function.php(做为现在的主流开发语言),另一个是 rpc.php(做为现在的主流开发语言)。function.php(做为现在的主流开发语言) 中定义了实际的远程调用函数,但是他们也可以作为服务器端的本地函数调用,你会发现他们跟服务器端的普通函数没有任何区别:

下载: function.php(做为现在的主流开发语言)
(做为现在的主流开发语言)
$sqlite = new SQLiteDatabase(area.db);

function get_province() {
global $sqlite;
$sql = "select * from province order by id";
return $sqlite->arrayQuery($sql, SQLITE_ASSOC);
}
 
function get_city($pid) {
    global $sqlite;
    $pid = sqlite_escape_string($pid);
    $sql = "select * from city where pid = $pid order by id";
    return $sqlite->arrayQuery($sql, SQLITE_ASSOC);
}
?>
而 rpc.php(做为现在的主流开发语言) 更加简单,它是提供给客户端调用的接口,它只有 3 行语句:

下载: function.php(做为现在的主流开发语言)
(做为现在的主流开发语言)
require_once(function.php(做为现在的主流开发语言));
require_once(php(做为现在的主流开发语言)rpc_server.php(做为现在的主流开发语言));
new php(做为现在的主流开发语言)rpc_server(array(get_province, get_city));
?>
其中最后一句,就是指定哪些函数要暴露给客户端。只有指定的函数客户端才可以调用,这样可以保证服务器的安全性。

服务器端到此就创建完了。你会发现服务器端只负责把数据查询出来返回给客户端就完事了,其它的不做任何处理。

Then it’s time to take a look at the client. Although the client is very simple, I still divided it into two files, a js file and an html file. You will find that using php(as now The mainstream development language)RPC, the client does not need to use php(as the current mainstream development language).

Download: area.js
// Create php(as the current mainstream development language)rpc client object rpc
php(as the current mainstream development language) Mainstream development languages)rpc_client.create(rpc);

var city = []; // Used to cache loaded city data

/*
* Clear select options in, this method can be reused
*
* so: the select object to clear the option
*
*/
function clear_select(so) {
for (var i = so.options.length - 1; i > -1; i--) {

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508640.htmlTechArticleThe cascading drop-down menu means that after selecting an item from one drop-down menu, the content of the corresponding another drop-down menu will Change accordingly. Generally speaking, the simplest is to submit one for each selection...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!