# Checking Username Availability with Mootools and Request.JSON

This tutorial will teach you how to check username availability with Mootools' Request.JSON and PHP/MySQL

In this tutorial you will learn how to use Mootools' Request.JSON function to check a username against an array or database. In this example we will be using a simple MySQL database. I will try to do as much hand holding as I can, but having a little experience with PHP and MySQL will be a plus.

### Tutorial Details

• PHP Server and MySQL Database required
• Difficulty: Beginner/Intermediate
• Estimated Completion Time: 30 - 45 Minutes

### Step 1 - The Setup

We are going to create a simple database and add a table. After that we will add a username into the database.

Woohoo, we have a database, a table and a single username. Let's get started with this tutorial!

### Step 2 - The Skeleton

For the first step we will create a simple page with one input field for the username and a submit button. Go ahead and open your favorite coding app - mine happens to be Coda - and create a new blank document named index.php. First I'll show you the end result code for this step and then I'll explain it in detail. I find that re-writting the code myself helps it stick, but you can copy and paste if you want.

Now we've got a pretty basic site layout. It will start to come together in step 2 so don't worry if it doesn't really look like much right now.

### Step 3 - A little Style

Feel free to style this up however you like, or use the CSS file from the source code.

### Step 4 - The Javascript Setup

We are going to need to include the Mootools framework into our php file. Without this, we can't use any of Mootools' classes or functions for our script. There are two ways of doing this. The first way is to use Google's AJAX Libraries API to link to the file. You can view the path Here. The Google libraries give you access to many frameworks so take a look around after you're done the tutorial. To speed things up, we can just use the piece of code below. Place this in your

The second way is to head over to the Mootools site

Now that we have the Mootools framework included in our file, we can go ahead and create a new file called main.js, or whatever you want to name it. Once this file has been created lets include it in our php file.

Go ahead and place that at the bottom of our php file. If you haven't already, let's open up our main.js file.

### Step 5 - The Mootools

This part can get a little tricky. Make sure the two files you have open are index.php and main.js as we will be moving back and forth between the files to give you a better idea of how Mootools interacts with the elements in index.php.

The first piece of code we are going to add to main.js tells our Mootools script to execute some code when the DOM has loaded.

Now that the main business is taken care of, we can get our hands dirty.

### Step 6 - Adding Events

We need to figure out a way to find out when a user has interacted with our user name input field. We do this using events. Events are Mootools' way of performing some action(s) when a user does something. This can include clicking on a link, pressing down a key, releasing a key, mousing over etc. For this example we are going to fire an event when the user releases a key in the username field.

Before we add the event, we need to give out user name input an ID. Without an ID, Mootools doesn't know which input we are talking about when we tell it to add an event.

Ok, I swear we will start coding some Mootools right now. Switch back to main.js. Inside your window event, add this code.

$('user_name') is what tells Mootools which element we are looking at. It relates to the elements ID. After that we use .addEvent() to tell Mootools that we want to do something at some point. The first option is the event that we are watching for. We are using keyup. There are tons of other events we can look for. You can read about them at W3C Schools. The last little bit holds a function that we will use to execute some JavaScript code whenever the event is fired. ### Step 7 - The Request Now that we have the event linked up, we can build the request to send when the event is fired. We are going to put this piece of code inside out event. Pretty simple looking, eh?! We start off by declaring our request. The first variable is url. This is the path to our PHP that houses our JSON magic. For now, just put in a path to a future file. The second variable is onSuccess. This is a function that is fired if our request is successful. This is where we will put most of our remaining code. Our last little bit, which is easy to look over is the .get($('signup')) variable trailing our request. This takes all our info from our form in index.php and sends it with the JSON request. Without this, the request isn't sending any data, and is pretty much useless. We've now made it useful!

We should probably make our JSON file right now so we don't run into any errors.

### Step 8 - The JSON

The concept behind our JSON file is pretty simple in theory. It takes our variables, does whatever we want with it, and then sends it back to our main.js file, all behind the scenes. This is awesome. Take a second to collect yourself.

Ok, now we are all calm, lets create a new file called json.php. If you added the path to your main.js file, name it the same and place it into proper folder. Open this sucker up!

Simple right? The first step is creating a null variable that we will be using as an array later on. Next, we need to connect to our database.

We should be all connected. Fill in your database information above. To make sure everything is running smoothly, point to your json.php file. If the page is blank, we are golden. If you see database connection errors, we have a problem. This is usually just a misspelled host/username/password. Double check!

All the variables sent to our json our sent as $_GET. If you've worked with forms before, this shouldn't be anything different. If you haven't worked with PHP forms before, please take a quick look at this page. We are putting the$_GET variable into a new variable to clean everything up. By wrapping the $_GET variable in the mysql_real_escape_string() function, we are making sure to combat mysql injection. Injection is bad! Next is the query. We are grabbing any rows from our MySQL database where the user_name row is equal to whatever the user has typed into the user_name input. If the query returns 0, there is no user name match. If there is no match, we add an action variable to our result array and give it a value of success. If there is a match, we simply give it a value of error. Finally, we add a user_name variable to the result array and give it a value of our$_GET variable and send it back to main.js using the json_encode() function.

When the JSON is encoded and sent back to the main.js file it looks like the code below

That ends the json file, you can save it and close it. You will not need it for this example anymore! Switch back to main.js

### Step 9 - Dealing with the Request

We've sent the request, we've checked if the user name exists and we sent the response. So what now? Now we are going to use Mootools to sort through the response and alert the user. How does main.js see what the response is? If we take a quick look back at our onSuccess function, you'll notice that the only variable passed in the function is response. This is the variable that now houses your JSON response. Please add this code inside our onSuccess function.

We haven't been able to test if our little application is even working right now, so lets take a second to do a quick test. Point your browser to index.php and type matt into your user_name input. Once you are done typing, matt an alert should popup saying Username Taken. You can now delete that alert; it is not needed anymore.

We have our styles, and we are receiving a response, lets change the inputs style depending on the response.

### Step 12 - Submission

Right now, even though we are saying that the user name is taken, the user can still submit the form. This means you would have to use PHP to do another level of error checking. You always want to make sure you are using PHP to make another level of error checking as it is possible for people to get around the disabling of the button. The more layers of security, the better! Open up index.php and find our submit button. Lets disable it!

We gave out submit button an id of submit_button so that Mootools can talk to it. Near the close tag we also added disabled. If you point your browser to index.php and try and click on the button, you will notice that nothing will happen. The form has been disabled. Open up main.js

If we add that piece of code in our response.success area, it will enable the button. Simply add it to the reponse.error section, change false to true and give it a test. When the user name is available, you should be able to click the button. When taken, the button should be disabled.

### Step 13 - The Cleanup

The user has inserted some text, and then deleted all the text from the input. If you notice all the alerts stay on the screen. This is a bit messy. We should probably fix that. Open main.js.

When the user clicks away from the input, it will fire the event blur. If the input box value is empty, we remove all classes, set the response p to empty and disable the button. Nice and tidy!

### The End

I hope this tutorial has helped to teach you some of the fundamentals when using simple JSON requests and interacting with elements on the page with Mootools. Feel free to download the source code! If you have any questions, follow me on Twitter!

One last note, the button styles used in this tutorial are the property of Zurb. Check them out, they do great work!