Wednesday, 31 January 2007
Article: Create an AJAX Process Using PHP and Oracle |
| |
|
| |
Larry Ullman, over at the Oracle web site has posted an article where he discusses the code necessary to use Ajax to go from a simple Web page to a JavaScript function to an XMLHttpRequest to a PHP script and, finally, to an Oracle database.
He gives the example of a registration form to drive home his idea. The theory is that he must confirm unique email addresses in the users table so that people cannot register multiple times using the same address. This is normally part of the validation process that comes after the user submits the form to the handling PHP script. But a user has to wait for this piece of validation. He says that a way should be devised where the users as soon as it enters their email address and goes to the next form field, the email address will be immediately validated. But to do so, one need to query the database, which is where Ajax comes in, he explains.
In the next step, he shows how to create a table to begin coding. He says there are two scripts involved and he lists out the following to develop and test everything for the process:
- Create the PHP script that queries the Oracle database
- Manually test the PHP script to see how it works
- Write the JavaScript code that interacts with the PHP script
- Make the HTML form that ties into the JavaScript
- Test the entire process
The rest of the article is divided in five steps with varied example codes. They are:
Step 1: Programming the Database Query
The entire PHP script is called ajax.php. The purpose of this script is to run a query in Oracle and print a message based upon the results of the query. This is all very basic Oracle, SQL, and PHP.The script expects to receive an email address in the URL, which is available in the variable ['email']. Then the script connects to the Oracle database. The query itself counts how many records in the users table have that email address, he explains. He shows this with the help of example codes.
Step 2: Testing the PHP Script
Because this entire AJAX process involves several technologies—HTML, JavaScript, PHP, SQL, and Oracle—it will be best off testing each piece as you go, he says. He cautions that if you don't, you can easily get lost trying to find where a problem exists. Testing this PHP script should be fairly easy he says and lists out the following steps to do it:
- Make sure you've created the table in Oracle and populated it
- Edit ajax.php so that it uses valid connection parameters for your Oracle installation
- Save the script as ajax.php
- Place it in the proper directory for your Web server
- Go to http://yoururl/ajax.php?email=X in your Web browser
He also says to use one of the email addresses that are already in the database in place of X. You should see the ‘Email address has already been registered!’ message. Then use an email address that has not been stored. You should see the ‘Email address is available!’ text. Once you have that working, you can move on to the actual Ajax aspect of this example, he explains.
Step 3: Programming the JavaScript
He says this section of the process is difficult unless one have worked on JavaScript earlier. In any case, the JavaScript code is the heart of the Ajax process, as it performs and handles the request from the PHP page. He explains the code in detail.
Step 4: Making a JavaScript-functional HTML Form
He says that the premise behind this example is a registration form that takes, at the least, an email address. To make it work he devises a set of example codes.
Step 5: Testing the Ajax Process
In this last step, he says having completed the HTML and JavaScript, save that as ajax.html, then place it in the same directory as the PHP script. Than the form should be loaded by running it in your Web browser, through a URL, but not by opening it from the file system (i.e, go to http://yoururl/ajax.html). Also, an unused email address should be typed in the proper input, then tab or click on the next input. The process should be repeated using a stored email address. He provides two screenshots to show the results.
|
| |
|
Read the Article
|
| |
|
|
| |
|
|
| |
|