结合AJAX进行PHP开发之入门5
点击次数:24 次 发布日期:2008-11-26 11:40:28 作者:源代码网
|
源代码网推荐 源代码网推荐 这个例子还说明了 Sajax 快速开发的另一个重要特性:不需要每个函数都有一个单独的文件,页面实际上调用的是其自身,因此更便于跟踪函数的调用(如图 5 所示)。x_foo_bar() 函数直接向页面发回 Ajax 请求,在请求中包含函数名和参数。关键是 sajax_handle_client_request() 函数,它截获所有的 Sajax 调用并自动对它们进行处理。 源代码网推荐
源代码网推荐 源代码网推荐 图 5. 使用 Sajax 客户机可通过一个页面访问服务器端的多个函数 源代码网推荐 源代码网推荐 源代码网推荐 将 Sajax 连接到相册 源代码网推荐 源代码网推荐 利用刚刚创建的代码,我们将用 Sajax 迅速把相册从多页面应用程序转化成活动的 Ajax 应用程序。 源代码网推荐 源代码网推荐 因为相册主要有两个函数,get_table() 和 get_image(),这也是需要用 Sajax 导出的全部函数。事实上,为了通过 Sajax 调用这些函数,这些函数本身基本上不需要修改,很快我们就会看到,我们只需要修改生成的链接即可。 源代码网推荐 源代码网推荐 清单 9. Sajax 相册的头部 源代码网推荐 源代码网推荐 <?php 源代码网推荐 require("Sajax.php"); 源代码网推荐 源代码网推荐 function get_image () { } // Defined later 源代码网推荐 function get_thumbs_table () { } // Defined later 源代码网推荐 源代码网推荐 // Standard Sajax stuff. Use Get, and export two 源代码网推荐 // main functions to javascript 源代码网推荐 $sajax_request_type = "GET"; 源代码网推荐 sajax_init(); 源代码网推荐 sajax_export("get_thumbs_table", "get_image"); 源代码网推荐 sajax_handle_client_request(); 源代码网推荐 ?> 源代码网推荐 源代码网推荐 对于本文而言,文档主体部分很简单。我们将使用 div 和 window 的 id 来显示服务器的输出。 源代码网推荐 源代码网推荐 清单 10. 显示服务器输出的 div 和 window id 源代码网推荐 源代码网推荐 <body> 源代码网推荐 <h1>Sajax photo album</h1> 源代码网推荐 <div id="window"></div> 源代码网推荐 </body> 源代码网推荐 源代码网推荐 最后还要编写 JavaScript 回调函数。该例中,因为所有的服务器输出都直接输出到 window div 标记,所以可以重复使用简单的回调函数。将回调函数添加到 Sajax 函数调用中,就可以得到头(head)。 源代码网推荐 源代码网推荐 清单 11. 简单的头 源代码网推荐 源代码网推荐 <head> 源代码网推荐 <title>Creating a Sajax photo album</title> 源代码网推荐 <style type="text/css"> 源代码网推荐 body { text-align: center } 源代码网推荐 div#window { margin: 0 auto 0 auto; width: 700px; 源代码网推荐 padding: 10px; border: 1px solid #ccc; background: #eee; } 源代码网推荐 table.image_table { margin: 0 auto 0 auto; } 源代码网推荐 table.image_table td { padding: 5px } 源代码网推荐 table.image_table a { display: block; } 源代码网推荐 table.image_table img { display: block; width: 120px 源代码网推荐 padding: 2px; border: 1px solid #ccc; } 源代码网推荐 img.full { display: block; margin: 0 auto 0 auto; 源代码网推荐 width: 300px; border: 1px solid #000 } 源代码网推荐 </style> 源代码网推荐 源代码网推荐 <script language="javascript"> 源代码网推荐 <? sajax_show_javascript(); ?> 源代码网推荐 源代码网推荐 // Outputs directly to the "window" div 源代码网推荐 function to_window(output) { 源代码网推荐 document.getElementById("window").innerHTML = output; 源代码网推荐 } 源代码网推荐 源代码网推荐 window.onload = function() { 源代码网推荐 x get table to window); 源代码网推荐 }; 源代码网推荐 源代码网推荐 </script> 源代码网推荐 </head> 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
