官术网_书友最值得收藏!

Chapter 2. Quick Tricks

PHP programmers can be tempted to consider only the server side of the website equation and leave the client side to the designer. Your job, as a PHP developer, is to make sure that the requested actions happen efficiently, and this does not appear to be a client-side problem.

When a customer expresses that the site doesn't look very modern or snappy, as a server-side developer, it is tempting to think that this is a problem for the designers, as they have traditionally had most of the control about what is sent to the client, and you have traditionally only been asked to supply an HTML skeleton for them to wrap the design around.

However, more and more code these days is shifting onto the client side of the transaction. And as a result, developers are taking a stronger interest in how they can affect data on the client side, and how they can help the designer to make an impact.

There are reasons why you, as a developer, should be interested in what happens on the client side. Using tricks such as Ajax and client-side form validation, you can relieve the strain on bandwidth and the server's resources, and at the same time, increase the responsiveness and usability of the application.

What happens on the client side does affect the server side. It is optimizing of this overlap, and not heavy and centralized architectures, that can help your own application scale with more ease.

And that's partly what's different about PHP/HTML versus PHP/HTML/jQuery. In the old PHP/HTML architecture, we had a very clear distinction, where data was calculated and turned into HTML on the server, and the client was used just for displaying that data exactly as the server rendered it. However, we are now at a point where we can use the power of client-side technologies, such as jQuery, to do part of the rendering on the client.

This allows your systems to grow easily, but it also allows your clients to have more control over how the data is rendered on the screen. With PHP and jQuery, your users will learn how quick and tactile their interfaces are, your marketers will love the "wow factor" that happens when they show the system to new customers, and your administrators will love you for relieving the burden on the server that classic "web 1.0" methods impose.

As PHP is used at the server side and jQuery at the client side, this book will show you a lot of tricks to help the interaction between the two. This is not a book about Ajax, as it is important that you don't concentrate on the data transfer part of it—instead, we'll concentrate on the jQuery and PHP part of it, and the Ajax part of it will be treated as a simple consequence of what we are doing.

jQuery makes Ajax almost invisible; in fact, it is so simple to use that after a while, you completely forget that what you are doing is something that could not be done easily even just four years ago. This is fantastic, as it allows us to combine the server side with the client side as if they are literally speaking to each other.

To start off, I'll show you a few tricks, which demonstrate how to relieve some more obvious resource wastes.

The examples discussed are the common patterns that you will have seen being used online or in desktop applications.

  • Dynamically populated select boxes, where changing one select box changes the options available in the other select box
  • Quick deletes, where deleting an object deletes it immediately from the view instead of waiting till a page has been reloaded, helping the site to appear responsive
  • Contextual help, where help can be obtained about an object by simply turning help on, and then clicking an icon near the object
  • Inline editing, where a table's data can be edited immediately, instead of needing to load a special forms page to edit it

In Chapter 1, Introduction and Overview, I described the example directory layout that will be used throughout the book.

To recap, here is the layout that I used while writing the following examples:

http://yoursite.xyz/jquery.min.js
http://yoursite.xyz/2-contextual_help/
http://yoursite.xyz/2-dynamic_select_boxes/
http://yoursite.xyz/2-inline_editing/
http://yoursite.xyz/2-quick_deletes/

When starting a new example, you should create a new directory. If the book says "create a file, xyz.html", then the file is created in that new directory.

In all the examples of this book, the library files, including jquery.js and all others we use, will be kept in the root of the examples, while the example files will be kept in their own separate directories.

This allows us to write new example files without needing to always download the library files.

主站蜘蛛池模板: 漾濞| 太和县| 濮阳县| 滕州市| 屯留县| 滕州市| 正阳县| 文山县| 和平区| 商城县| 旬邑县| 寿阳县| 浙江省| 双牌县| 定日县| 都江堰市| 罗源县| 文昌市| 华池县| 芜湖县| 太和县| 辽源市| 潞城市| 崇文区| 开鲁县| 禄劝| 道孚县| 江永县| 宣化县| 皋兰县| 固安县| 托克逊县| 赤城县| 荆门市| 仙游县| 万盛区| 西城区| 正阳县| 忻州市| 安溪县| 子洲县|