Background
Previously I posted my PHP AJAX Framework so I thought I would give an example of its use. In this case, you probably have seen this example before - using AJAX and PHP to check if a username is available during some kind of web-based user registration. The goal is to alert the user if a username is taken before they have submitted the form, allowing them to enter another username quickly. This feat can be easily accomplished with the PHP AJAX Framework.
Setup
You are going to need to copy the contents of the PHP AJAX Framework into a JavaScript file. In this example I have put the contents into the “php_ajax_framework.js” file. You are also going to need to write a PHP script that will be called in the AJAX sequence of events, notifying the user whether the username is available or not.
Below is an example PHP Script, which has the filename “username_exists.php” and will need to be in the same directory as the page that is triggering the AJAX.
PHP Username Available Script
This script is requested by our AJAX helper function and sent the text in our username input box. In the script is a call to a function usernameExists() which returns true or false, depending on whether the username exists. In a real web site or application, this would query our database to see if the username existed. In this example, for ease of understanding, we just check to see if the username is in a defined array of usernames.
<?php $username = trim($_GET['username']); if (usernameExists($username)) { echo '<span style="color:red";>Username Taken</span>'; } else { echo '<span style="color:green;">Username Available</span>'; } function usernameExists($input) { // in production use we would look up the username in // our database, but for this example, we'll just check // to see if its in an array of preset usernames. $name_array = array ('steve', 'jon', 'george', 'admin'); if (in_array($input, $name_array)) { return true; } else { return false; } } ?>
Username Check Page
Our example username page is very basic - you should integrate this into your existing site. The important parts are the JavaScript functions and the input box, which uses the onKeyUp() JavaScript event to call a function which them triggers the AJAX chain of events. More information on setting up the PHP AJAX Framework can be found on its post page, but the basic idea is that you create an _init function that specifies which PHP script to execute and what variables to send it (in this case, the value of the user input box.) You also create a _results function which will be given the results of your PHP Script and then do something with them (in this case, display a notice to the user whether the name is available.) The _init and _results functions are given function names prepended to these terms which are the same (in the below case, we prepend “check_username” to both) and then they are triggered by calling our PHP AJAX Framework function ajaxHelper() with the name we prepended.
<html> <head> <script type="text/javascript" src="php_ajax_framework.js"></script> <script type="text/javascript"> function check_username_init() { var user_field = document.getElementById('user'); return 'username_exists.php?username=' + user_field.value; } function check_username_ajax(results) { var results_div = document.getElementById('results_div'); results_div.innerHTML = results; } function check_username() { var user_field = document.getElementById('user'); if (user_field.value.length > 0) { //call our AJAX function in the PHP AJAX Framework ajaxHelper('check_username'); } else { //clear results field var results_div = document.getElementById('results_div'); results_div.innerHTML = ''; } } </script> </head> <body> Username: <input name="user" id="user" size="20" onKeyUp="check_username();"> <div id="results_div"></div> </body> </html>
Here is an live example of the above scripts all working together. Remember that we are just checking against an array of names. Try typing in one of the names in the array to see what happens when a particular username is taken (steve, jon, george, admin.)
Conclusion
Implementing AJAX is easy and just requires putting all the pieces together. Give AJAX and shot and implement the PHP AJAX Framework on your site for simple AJAX tasks.


