Beginner's Javascript & AJAX
( concentrating on
getElementById method and
innerHTML property )
Andy Griffiths
The positives using Javascript
- Immediate feedback to the end-user
- Good for simple validation
- Executes in browser - no traffic to server (unless specifically calling a script from server)
The negatives using Javascript
- Extra hassle duplicating validation already done in php
- May impact on Search Engine results when used for navigation.
- Efforts will be ignored by text browsers or browsers with Javascript disabled
A practical example
The demo
Disclaimer:
The code in this example is in no way meant to suggest the "best" method of implementing these processes.
This was a first attempt using javascript. The best that can be said is - it works!
How does it hang together?
The demonstration used 3 files:
- The HTML page
- The Javascript which does the work
- The PHP code executed by the AJAX call
The html
The key to the process is defining an id to an HTML "open" tag which is used by the javascript (defined prior to the /body tag).
This code uses the span tag
The Javascript doing the work (part 1)
This chunk of the included javascript loops through each form on the page and each element within the forms, checking for a defined event on specific fields, triggering the execution of the function "My_events"
The Javascript doing the work (part 3)
This section of the javascript code is the ajax call to a php script.
The php script called via AJAX
The wrap-up
Worth the investment of time and effort to provide a more effective and user-friendly interface
At the same time reduce some of the traffic / load on your server
Code must be tested with Javascript disabled on the browser to ensure code "degrades gracefully"
Full source code used in demo available DOWNLOAD