<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jimi Sulaiman &#187; Javascript</title>
	<atom:link href="http://www.jimisulaiman.com/category/codes/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jimisulaiman.com</link>
	<description>Online Portfolio</description>
	<lastBuildDate>Sat, 10 Sep 2011 00:20:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Using Ajax to validate form</title>
		<link>http://www.jimisulaiman.com/2010/02/using-ajax-to-validate-form/</link>
		<comments>http://www.jimisulaiman.com/2010/02/using-ajax-to-validate-form/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 18:53:52 +0000</pubDate>
		<dc:creator>Jimi Sulaiman</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Source Files]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MySql]]></category>

		<guid isPermaLink="false">http://www.jimisulaiman.com/?p=475</guid>
		<description><![CDATA[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&#8217;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 &#8220;Username already taken. Please try a different username.&#8221; 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&#8217;m using PHP 5 and MySQL 5) You&#8217;ll gonna need two files: - form.php &#60; this will be your form - check_username.php &#60; 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&#8217;re done on this page. I&#8217;ve commented the code below to explain what each line does.  I&#8217;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() { $(&#34;#myform&#34;).validate(); //apply the validation plugin to your form $(&#34;#username&#34;).rules(&#34;add&#34;, { remote: { url: &#34;check_username.php&#34;, //place the PHP here type: &#34;post&#34;, //using the post method to send data (you may also use the get method) data: { username: function() { return $(&#34;#username&#34;).val(); //get the value from the text input } } }, messages: { remote: &#34;Username already taken. Please try a different username.&#34; //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&#8217;t exist or false otherwise.  Here is the search Query in PHP for this example. $query=&#34;SELECT * FROM users WHERE LOWER(username) = '$username'&#34;; $result=mysql_query($query); $num=mysql_num_rows($result); //check if number of users with the username exist more than 0 if($num &#62;0){ echo &#34;false&#34;; }else{ echo &#34;true&#34;; } 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:]]></description>
		<wfw:commentRss>http://www.jimisulaiman.com/2010/02/using-ajax-to-validate-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

