Listing 12.7. SQL Code to Create a User's Table (awad_users.sql)
Listing 12.8. Bridging the Gap Between the Client and the Server (ServiceConnector.php)
As I said, this file is extremely simple, yet it packs a lot of power and can basically be used in any situation where you need to make this sort of connection by simply including another object. The object that we are using in this case is for user management, which will be incorporated into our final sample. The methods it contains will look very familiar since we already created the requests in our UserValidation object. The methods this object consists of are listed in Table 12.1.
Before we create the constructor function, we first need to create a private local class variable called dbConnector, which we will use to get the Singleton instance of the database connector object from the DatabaseConnector.class.php in the constructor. After we create this variable and set it to the Singleton object, we fire its init method to make the initial connection and select our database. This object can be used in each of our other methods to connect to the database and make queries. Listing 12.9 shows this code in action.
Listing 12.9. Creating the Object Constructor (UserManager.class.php)
A very important thing to remember in order to connect to your database is to change the static variables in Constants.class.php.
These variables look like the code in Listing 12.10.
Listing 12.10. Changing the Database Connection Information (Constants.class.php)
These variables should contain the connection information for your database. These are the only items that you will have to modify in Constants.class.php in order to get the sample to work on your server. For security reasons it is also a good idea to put the Constants file in a secure directory that is not accessible from the Web. DatabaseConnector.class.php will never need to be modified because it is configured to connect to any database with this information.
Verifying User Information
All of our XHRs for client-side validation were made to this method. If we take a look back at the Ajax requests that we created in our UserValidation object, they specify the UserManager object, the method name, and the parameters necessary to verify data against the database. After the connection has been made between the client and object, the parameters are used to select the specified data from the database to see whether there is a match. If there is a match, the method returns true; otherwise, it returns false. You will notice we are returning these messages in the form of XML through the Constants.class.php file. This is to make the response a valid DOM object, which will then be accessible via Ajax. See Listing 12.11 for an example.
Listing 12.11. Verifying User Information in the Database (UserManager.class.php)
Registering and Logging In a User
Although these methods do not have anything to do with Ajax, they do allow us to obtain users on which to run our validation requests. Therefore, I am including them in order to get us started. The first method I am displaying is the register method in Listing 12.12.
Listing 12.12. Registering New Users (UserManager.class.php)
This method simply takes the data that was entered into the form in our index.php file and adds it to our database.
The register method also fires a method called insertWelcomeMessage, which is not covered in this chapter, but is included in the code sample. This method creates a welcome email by default for new users.
The next method is a login method for existing users (see Listing 12.13).
Listing 12.13. Logging In Existing Users (UserManager.class.php)
These two methods are fired from the index.php file when the form posts back on itself. Listing 12.14 shows the code that needs to be added to the top of our index page to handle registration and login.
Listing 12.14. Handling User Logins and Registration (index.php)
When the form is posted, it posts back on itself and runs the PHP at the top of the page. It first sets the error variable for later use and then checks the mode of the form. If the mode is equal to register, it runs the register code; otherwise, it runs the login code. In the registration code, it verifies that all the fields were completed and then registers the user through the UserManager object. If the fields are not completed, we set an error message to display to the user. In login mode, we verify that the Username and Password fields are completed and, if they are, we log in the user through the UserManager object. After we receive a successful response, we redirect the user into the application. We set an error message again if there are any failures during this process.
Now that we have created this code, we can really use it for any web application. This reusability is the great thing about writing server-side classes and client-side objects.