Introduction to PHP development with AJAX (3)

Source: Internet
Author: User

Connect Sajax to the album

With the code we just created, we will use Sajax to quickly convert the album from a multi-page application to an active Ajax application.

Because the album mainly has two functions: get_table () and get_image (), which are all functions to be exported using Sajax. In fact, to call these functions through Sajax, these functions basically do not need to be modified. Soon we will see that we only need to modify the generated link.

Listing 9. Head of the Sajax album

<? 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 ();
?>

For this article, the main part of the document is very simple. We will use the div and window IDs to display the server output.

Listing 10. display the div and window id output by the server

<Body>
<H1> Sajax photo album
<Div id = "window"> </div>
</Body>

Finally, you need to write JavaScript callback functions. In this example, because all server outputs are directly output to the window div tag, simple callback functions can be reused. Add the callback function to the Sajax function call to obtain the header (head ).

Listing 11. Simple Headers

<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>

The last step is to ensure that all links in the application are customized Sajax calls. Just take the code in the previous section and replace it with the following: href = "index. php? Start = 0 & step = 5 "changed to onclick =" x_get_table (0, 5, to_window) ", href =" expand. php? Index = 0 "to onclick =" x_get_image (0, to_window )".

And make the same changes in the corresponding function: get_image_link () and get_table_link (). This completes the conversion to Sajax (6 ). All links are converted into JavaScript calls corresponding to remote PHP calls. PHP uses the JavaScript response handler to_window () to directly output to the page.

The entire application is included in a page, and other functions (get_table (), get_image (), and so on) can be placed in a separate library file that cannot be accessed from the Web. In most Ajax applications, each request sent to the server must be processed by a separate script, or at least a very large handler script must be written to redirect requests. It may be very troublesome to gather all these files together. Sajax always requires only one file. In this file, you only need to define the functions we use. Sajax replaces the processing script.


We can see that the URL remains unchanged and brings more pleasant user experience. Window div is displayed in a gray box, and the content generated by Sajax is very clear. The script does not have to know itself or its location on the server, because all links eventually become directly called to the page's own JavaScript. Therefore, our code can be well modularized. We only need to keep the JavaScript and PHP functions on the same page, even if the page location changes.

Extended album

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.