AJAX is a way of using JavaScript to process user requests made on a website. Normally when a user fills out a form or clicks a link the browser will send a request to the server and browser will read the data the server sends back effectively leaving the page they are looking at and loading another page (normally called a post or get request). With AJAX the developer can specify an area on the page such as a frame, div layer or even paragraph they wish to use to display dynamic information.With AJAX when a user clicks a link or fills out a form JavaScript will do all the work. Instead of giving the button or link a normal command like:
HTML:
-
method="post" action="somepage.php"
Is replaced with:
HTML:
-
action="JavaScript:someFunction('someData', 'toSomeArea');"
This will then call a JavaScript function which will take the user data and send their data to an external script though the JavaScript command "XMLHttpRequest" once the data has been sent via JavaScript its response will then be displayed the area set-up by the developer. All this is done without the user leaving or having to refresh the page.
The advantages of this are:
- It provides a more streamlined user experience
- Its usually quicker to get a response (as the AJAX handler only needs to send back the raw data without having to give any style information or only very basic HTML)
- Its a more interactive experience, allowing user to interact with it like it was a proper user interface and not just a website
- You can create rich applications in it such as Google Maps and Google Mail
The main disadvantages of AJAX are:
- It will only function in modern browsers
- It will only work if the user hasn't disabled JavaScript
- It wont work on most mobile devices
- Most of the time it will not be accessible to disabled users
I have been experimenting with AJAX on my holding page currently I have a "Keep me Posted" section allowing users to enter there email address which I will use to email users on updates to the site. This uses a form to send their email address to a PHP AJAX sub handler page which will validate the user input making sure they have entered an email address in the correct format and then save this to a database and also make sure that their email address is real by sending them an automated email asking them to click a link to activate their email address in the database.
Once the JavaScript has sent the data to the PHP sub handler the script will then respond with either a thank you message or enter a valid email address message without having to refresh/reload the page.
Its quite a nice touch, so I will be continuing to explore it potential to see how I can use it to improve my website.