Using Ajax to validate form

Posted February 6th, 2010 in Javascript, PHP, Source Files, Uncategorized by Jimi Sulaiman

There are times where you need to validate a form to a data stored in a database without the need to submit the form beforehand. The solution -

Ajax (shorthand for asynchronous JavaScript and XML).

For example, you are creating a user registration form where obviously, the username to choose from needs to be unique. Instead of validating the username after form submission, wouldn’t it be much friendlier if the form can validate itself  from the server asynchronously in the background from a simple event such as on submit or unfocus event?

In this post, I will be showing how you can the validate with Ajax using PHP/MySQL as your back-end. A table called user is created in the MySQL database which contains some dummy usernames.  When user enters a username existed from this table, the form will prompt an error with message “Username already taken. Please try a different username.”

What you need:

- jQuery 1.4, my favourite javascript library. Click here to download
- jQuery plugin, Validation 1.6. Click here to download
- PHP and mySQL enabled web server (for this example, I’m using PHP 5 and MySQL 5)

You’ll gonna need two files:

- form.php < this will be your form
- check_username.php < this will check  if data exist in the user table in your MySQL database

Link jQuery and the plugin to the form page first.  You can refer to the source file(link at the bottom of this post) if you are not clear on how to do this.  Then add the javascript code below and you’re done on this page.
I’ve commented the code below to explain what each line does.  I’m not gonna touch on the Validation plugin here but if you are interested, you can view its Documentation available from the download site.


$(document).ready(function() {

    $("#myform").validate(); //apply the validation plugin to your form

	$("#username").rules("add", {
		 remote: {
			url: "check_username.php", //place the PHP here
			type: "post", //using the post method to send data (you may also use the get method)
				data: {
				  username: function() {
					return $("#username").val(); //get the value from the text input
				  }
				}
			},
		 messages: {
		   remote: "Username already taken. Please try a different username." //set the message you want to display
		 }
	});

});

The next step is to create the PHP page that checks if the user existed.  Because the validation only works with Boolean values,  so you will need to code the page to return the text true if username doesn’t exist or false otherwise.  Here is the search Query in PHP for this example.

$query="SELECT * FROM users WHERE LOWER(username) = '$username'";
$result=mysql_query($query);
$num=mysql_num_rows($result);
//check if number of users with the username exist more than 0
if($num >0){
echo "false";
}else{
echo "true";
}

Test the form by typing any username that has already existed, your form should be showing an error message next to the username input as shown in the demo below.

Click here to view live demo

Get the complete source file: Ajax Form Example (99)

  • Share/Bookmark

My First DSLR Camera

Posted July 19th, 2009 in Uncategorized by Jimi Sulaiman

I’ve been wanting to upgrade from the point and shoot camera to a DSLR  (entry level at least)  and finally managed to buy one a few days ago - A Nikon D5000. Being denial for a few years that my point and shoot camera can achieves what a DSLR can do(I photoshop alot) but on the day I’ve got my hands on it, I was totally blown away! It was like having a new sets of color or brushes to paint on a bigger canvas!

I don’t have time to go out and put on extensive tests with the camera yet but here’s a few shots I that I did so far. All pictures below were taken using 18-55mm f/3.5-5.6G VR Lens (kit lens) – since this is the only lens I’ve got for now, I don’t expect to achieve the best results from these tests. None of these images were photoshopped.

Focus Test

Subject Focused

Subject Focused

Background Focused

Background Focused

Using Natural Light Source

05

The weekend - 24mm; IS0 320; 1/25 sec; F/4; +0.7

06

I Will Shoot You - 48mm; IS0 400; 1/30 sec; F/5.3; +0.7

Macro Test

08

The Cap - 48mm; IS0 640; 1/6 sec; F/5.6; +0.7

02

Wild Flowers - 55mm; IS0 320; 1/320 sec; F/10

Miscellaneous

With Flash; 38mm; ISO 200;

With Flash; 38mm; ISO 200;

Optimus

Optimus - 32mm; IS0 500; 1/25 sec; F/4.8

  • Share/Bookmark

Retiring from Freelancing

Posted March 9th, 2009 in Uncategorized by Jimi Sulaiman

After 6 years of freelancing on website design and development, I have come to a point where I feel that I’ve reached my goal. During these years, I honestly can say that its not worth the money (it can’t really pays the bill), but the experiences and knowledges earned were priceless and personally rewarding. I’m not ditching any of my current clients, I’m just gonna stop doing new projects thats all.

My next project will be something that I want to do, that challenges me, and that is exciting and new - It doesn’t has to be IT stuff. It could be painting or cooking or designing as long as I get to be the one who is ‘in charge’, someone who desides things this time.

  • Share/Bookmark

Islamic Chinese Calligraphy

Posted August 29th, 2008 in Uncategorized by Jimi Sulaiman
by Soleh Yu Jin Xue

by Soleh Yu Jin Xue

We’ve purchased this today from the calligrapher himself. Its amazing :)

more about the calligrapher (link updated)

  • Share/Bookmark