I've never used AJAX since it was never a necessity, but I am considering learning AJAX since it seems to make things a lot easier and generally make websites flow a bit better from my understanding. Despite having a general sense of the advantages of AJAX, I'm not too familiar with how it would function within the confines of an online storefront.
Right now, some of my pages function as the below:
//login.php $_POSTs data to login-submit.php, which redirects to index.php
//cart.php $_POSTs data to checkout1.php
//checkout1.php is then followed by checkout2.php, checkout3.php
//basically any page that deals with SQL has a separate ~-submit.php page
//my current storefront has 101 .php pages, about 45 are ~-submit.php pages
//catalogue.php must $_POST to cart.php every time an item is added
//this means time is lost when you're adding many items to cart
My understanding is that utilizing AJAX means no redirection needs to happen (eg. login.php submits data to itself
, meaning less pages are needed, particularly no need for an interstitial ~-submit.php
page). More importantly, an application I can think of is not having catalogue.php
$_POST
data to cart.php
every time you click "Add to cart"; instead you could click "Add to cart" and remain on the same page since you could just store in $_SESSION
the item in question on the same page.
Is this the only benefit AJAX would provide in such a site?
edit; to further clarify, some of the things I think AJAX would do for my site are below:
//~-submit.php page code is the same, but executed onclick of a form button without redirects
//drops overall page count of site by almost half
//similar for the cart, allow users to remain on the same page and add stuff to cart
//rather than adding to cart, back button, adding to cart, back button, etc
Am I right in assuming AJAX can accomplish this? My overall code remains largely unchanged, but with the addition of AJAX I can expect a more fluid experience.
Essentially you're right, it allows the site user to make interactions with a page which are then fed through to your server-side code to process. So you could combine all three of your checkout pages onto a single page if you wanted to.
It's worth bearing in mind that because the user isn't changing page, they won't be able to use the back button in the same way as they would using an AJAX free site. It's also important to realise that content loaded through AJAX isn't automatically indexed by Google, which can have SEO implications. There are of course ways around both of those limitations.
Don't overuse AJAX for the sake of it. If you think it will genuinely improve the user experience, then use it but it's not always an improvement.