Home > Backend Development > PHP Tutorial > Introduction to PHP development with AJAX (3)_PHP tutorial

Introduction to PHP development with AJAX (3)_PHP tutorial

WBOY
Release: 2016-07-13 10:47:35
Original
981 people have browsed it

将 Sajax 连接到相册

  利用刚刚创建的代码,我们将用 Sajax 迅速把相册从多页面应用程序转化成活动的 Ajax 应用程序。

  因为相册主要有两个函数,get_table() 和 get_image(),这也是需要用 Sajax 导出的全部函数。事实上,为了通过 Sajax 调用这些函数,这些函数本身基本上不需要修改,很快我们就会看到,我们只需要修改生成的链接即可。

  清单 9. Sajax 相册的头部

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


Sajax photo album





Finally, you have to write a JavaScript callback function. In this example, because all server output goes directly to the window div tag, the simple callback function can be reused. By adding the callback function to the Sajax function call, you get the head.

Listing 11. Simple header


Creating a Sajax photo album





The final step is to ensure that all links in the application are custom Sajax calls. Just take the code from the previous section and replace it as follows: href="index.php?start=0&step=5" becomes onclick="x_get_table(0, 5, to_window)", href="expand.php?index =0" becomes onclick="x_get_image(0, to_window)".

​And make the same modifications in the corresponding functions: get_image_link() and get_table_link(). In this way, the transformation to Sajax is completed (as shown in Figure 6). All links become JavaScript calls corresponding to remote PHP calls, which output directly to the page using the JavaScript response handler to_window().

The entire application is contained in a single page, and the rest of the functionality (get_table(), get_image(), etc.) can be placed in a separate library file that is not accessible from the web. In most Ajax applications, each request to the server needs to be handled by a separate script, or at least a very large handler script needs to be written to redirect the request. Bringing all these files together can be cumbersome. Using Sajax will always require only one file, in which we simply define the functions we use. Sajax replaces handler scripts.


​You can see that the URL remains the same and brings a more pleasant user experience. The window div is displayed in a gray box and the content generated via Sajax is very clear. The script doesn't necessarily have to know itself or its location on the server, because all links end up being JavaScript calls directly to the page itself. Therefore our code can be well modularized. We just need to keep the JavaScript and PHP functions on the same page, even if the page position changes.

Expanded photo album

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632869.htmlTechArticleConnecting Sajax to the photo album Using the code we just created, we will use Sajax to quickly convert the photo album from a multi-page application into an active Ajax application. Because the photo album mainly contains two letters...
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