Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello, friends! Please kindly note that I eagerly read Larry's book on Javascript, but as a beginner, I struggled with the AJAX and Ajax-related chapters. As a result, I purchased and read--and loved-- Larry's book on AJAX.

 

However, and please forgive me for the lack of wherewithal, but I had some very basic questions:

1. What are the benefits of using AJAX and Javascript for form submissions/validation as opposed to simply relying on PHP? Is the benefit remarkable or catered to certain applications?

 

2. What are the various applications that Ajax allows for that could not be achieved with PHP alone?

 

Thank you very much for your kind and helpful responses.

Link to comment
Share on other sites

Probably the most important thing to understand is that Ajax is not a replacement for PHP; it's a technology that combines JS with a server-side language (e.g., PHP) to do things that can only be done on the server side, but without the need to reload the page. That's really the whole point of it; to create a more seamless, desktop application-like experience for the user. Also, it's important to understand that JS and Ajax are not one and the same.

 

To more specifically answer your questions:

1) Because JS is client side, you can use JS to perform some basic form checking before submitting the form to the server and your PHP script. By doing this, the user will get an instantaneous response to any mistakes they may have made in filling out the form. This generally equates to a better, more-responsive user experience.

 

Some important things to note with JS validation though are that because anyone can view your JS code, you don't want to validate with only JS, as people might try to study your code to find ways around your validation for the sake of hacking your site/DB. Also, PHP has much more robust functions for performing validation and preventing DB attacks, so PHP validation should always be used and the final line of defense, regardless of whether or not you are also using JS for validation.

 

Some good examples of when to perform JS validation would be to check that an entered password meets the site requirements, zip codes are entered in the proper format, etc., etc. This will help prevent a majority of the honest mistakes that users might make in filling out a form.

 

The more you can entrust to the client side, the faster and better the user experience and the less of a strain you put on the server. Just remember to never sacrifice safety for speed.

 

2) Again, Ajax is not a replacement for PHP, and honestly, Ajax is never required for a site, but it makes the whole experience on the site a lot smoother and easier to use. The purpose of Ajax has always been to create a more desktop application-like experience for web pages.

 

To give a couple of examples of how to effectively use Ajax:

- You could use Ajax to poll (i.e., check) a DB every so many seconds, and then update content on the site whenever there is a change to the DB. By doing this, you can constantly update a site's content without the user having to refresh the page. This is good for things like stock tickers, updating the scores of sporting events, etc., etc.

 

- You can use Ajax to update only part of a page. For example, with most websites, when you click on a link, the new page that is loaded will likely have a lot of the same content as the old page. However, with Ajax, when a link is clicked, you can choose to change only a small part of the page while leaving everything else untouched. This ultimately puts less of a strain on the server (because less info needs to be processed/downloaded) and it'll make the new "page" load faster for the user, and without the need to actually reload the page.

 

With all that said, please note that there are two big downsides to Ajax:

- It "breaks" the back button in your browser in that because there are no page transitions (at least, not according to the browser), the back button will take you to the page you were on before you loaded the site, and not the previous page you were on on the site.

- Similar to the above, you cannot bookmark pages because there are no page transitions.

 

Over the years, people have come up with ways to circumvent these two problems, but they're all half-ass workarounds that can be tricky to implement and don't really solve the problem.

 

The silver lining to all this is that there is a new API in HTML5 called the History API, which allows you to properly create proper URLs for Ajax-driven sites, and thus the back button and bookmarking problems can be resolved for Ajax sites. Unfortunately, the History API is still new and does not have universal support, especially among IE browsers.

 

Also, in what is quickly becoming a new replacement for Ajax in some situations, there is also the HTML5 WebSocket API, which allows for true, asynchronous communication between two endpoints. This allows you to do things via a browser that before were impossible and/or very inefficient using only Ajax.

For example, you could use the WebSocket API to create a chess application that detects when your opponent makes a move and instantly updates the board on your end. You could also use the WebSocket API for things like chat applications, etc.

Gmail and Facebook already very actively use this technology for their chat applications.

 

Well, hopes that is somewhat the answer you were looking for.

  • Upvote 3
Link to comment
Share on other sites

Great response! HartleySan, thank you so very much for the thorough and kind response! I see why Larry's so grateful to you for all the assistance you provide.

 

I'm not sure what an API is, however. I'll be sure to look it up.

 

By the way, I've been reading Larry's new Advanced PHP book, which is remarkably written with exellent choices of exercises. I couldn't be more impressed.

Link to comment
Share on other sites

I think of an API as something that either allows you to do something that you couldn't do before or allows you to more easily do a task that was difficult to do in the past.

 

An API is generally made up of a bunch of functions/methods written by someone else that you can readily call to easily accomplish various tasks.

Link to comment
Share on other sites

  • 2 months later...
 Share

×
×
  • Create New...