<?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>Roshan Bhattarai's Blog -  A blog about PHP, Ajax, JavaScript, CSS and Web 2.0 &#187; ajax</title>
	<atom:link href="http://roshanbh.com.np/category/ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://roshanbh.com.np</link>
	<description>Useful Tutorials, Scripts , Tips, and Resources for all PHP and Ajax beginners and experts .</description>
	<lastBuildDate>Thu, 10 Jun 2010 11:38:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>6 free ajax chat applications using PHP</title>
		<link>http://roshanbh.com.np/2008/09/free-ajax-chat-applications-php.html</link>
		<comments>http://roshanbh.com.np/2008/09/free-ajax-chat-applications-php.html#comments</comments>
		<pubDate>Tue, 23 Sep 2008 12:02:37 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ajax chat]]></category>
		<category><![CDATA[chat]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/?p=279</guid>
		<description><![CDATA[While talking about chat application, these days people hate those kind application made in PHP which need page refreshing. In this post, I&#8217;ll show you six different free Ajax chat applications which might be very useful if you&#8217;ve to use Ajax based chat application. Depending upon the requirement of your chat application, you can use [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F09%2Ffree-ajax-chat-applications-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F09%2Ffree-ajax-chat-applications-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>While talking about chat application, these days people hate those kind application made in PHP which need page refreshing. In this post, I&#8217;ll show you six different free Ajax chat applications which might be very useful if you&#8217;ve to use Ajax based chat application. Depending upon the requirement of your chat application, you can use them. I might have missed another good Ajax based chat application, if you find any the please post it to the comments.<br />
<span id="more-279"></span></p>
<h4><a href="https://blueimp.net/ajax/" target="_blank">Ajax Chat from http://blueimp.net</a></h4>
<pre style="overflow:auto"><a href="https://blueimp.net/ajax/" target="_blank"><img title="ajax-chat-blueimp" src="http://roshanbh.com.np/wp-content/uploads/2008/09/ajax-chat-blueimp.png" border="0" alt="Free Ajax chat application using PHP" /></a></pre>
<p>A powerful Ajax chat application with a lot of features at free of cost. Customize the layout and Use it in your project.</p>
<h4><a href="http://ajaxim.com/" target="_blank">Ajax- IM </a></h4>
<pre style="overflow:auto"><a href="http://ajaxim.com/" target="_blank"><img title="ajax-im" src="http://roshanbh.com.np/wp-content/uploads/2008/09/ajax-im.gif" border="0" alt="" /></a></pre>
<p>It is not a only a chat application, it is a instant messenger that&#8217;s why it is named as Ajax-IM. Very useful for implementing in community based websites.</p>
<h4><a href="http://www.phpfreechat.net/" target="_blank">PHPfreeChat </a></h4>
<pre><a href="http://www.phpfreechat.net/" target="_blank"><img class="alignnone size-full wp-image-280" title="ajax-chat-phpfreechat" src="http://roshanbh.com.np/wp-content/uploads/2008/09/ajax-chat-phpfreechat.gif" border="0" alt="" width="400" height="251" />
</a></pre>
<p>Another great Ajax chat application build in PHP.Very easy to customize and use. You can even create rooms for chatting as well.</p>
<h4><a href="http://www.sterryit.com/chatr/" target="_blank">Chatr </a></h4>
<pre><a href="http://www.sterryit.com/chatr/" target="_blank"><img class="alignnone size-full wp-image-282" title="chatr" src="http://roshanbh.com.np/wp-content/uploads/2008/09/chatr.gif" border="0" alt="" width="400" height="227" /></a></pre>
<p>Another simple ajax chat application with limited functionality built in PHP.</p>
<h4><a href="http://www.ajaxchat.org/About/AboutFreeChat.html" target="_blank">Ajax chat from Ajax-chat.org </a></h4>
<pre><a href="http://www.ajaxchat.org/About/AboutFreeChat.html" target="_blank"><img class="alignnone size-full wp-image-283" title="ajax-chat-org" src="http://roshanbh.com.np/wp-content/uploads/2008/09/ajax-chat-org.gif" border="0" alt="" width="400" height="181" /></a></pre>
<p>Very simple, lightweight and easy to use another Ajax chat application built in PHP.</p>
<h4><a href="http://blogoscoped.com/chat/help.php5" target="_blank">Ajax chat from Blogoscopted </a></h4>
<pre><a href="http://blogoscoped.com/chat/help.php5" target="_blank"><img class="alignnone size-full wp-image-284" title="blogoscopted-ajax-chat" src="http://roshanbh.com.np/wp-content/uploads/2008/09/blogoscopted-ajax-chat.jpg" border="0" alt="" width="360" height="261" /></a></pre>
<p>Fancy and avatar based ajax chat application built in PHP. As you can see in the above image, it looks like you&#8217;re really inside a chat room for chtting.</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=279&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/09/free-ajax-chat-applications-php.html/feed</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Solving European characters (Western charset) problem with Ajax and PHP</title>
		<link>http://roshanbh.com.np/2008/09/solving-european-characters-western-charset-problem-with-ajax-and-php.html</link>
		<comments>http://roshanbh.com.np/2008/09/solving-european-characters-western-charset-problem-with-ajax-and-php.html#comments</comments>
		<pubDate>Mon, 15 Sep 2008 18:32:20 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips and technique]]></category>
		<category><![CDATA[ajax problem]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/?p=276</guid>
		<description><![CDATA[Today, I would like to tell you how to handle character set problem which occurs during the data fetched from PHP using Ajax mainly on the western characters(European charset).Lots of people asked me about this problem where these European charset is displayed in unreadable format after fetching it from ajax. Problem with European Charset with [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F09%2Fsolving-european-characters-western-charset-problem-with-ajax-and-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F09%2Fsolving-european-characters-western-charset-problem-with-ajax-and-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Today, I would like to tell you how to handle character set problem which occurs during the data fetched from PHP using Ajax mainly on the western characters(European charset).Lots of people asked me about this problem where these European charset is displayed in unreadable format after fetching it from ajax.<br />
<span id="more-276"></span></p>
<h4>Problem with European Charset with Ajax and PHP</h4>
<p>Suppose that I&#8217;ve the following code in PHP file, which output the string which contain the european characters.</p>
<pre class="prettyprint" style="overflow:auto">&lt;?php
  $str="€-accentuée";
  echo $str;
?&gt;</pre>
<p>Now, let&#8217;s use the Ajax from JavaScript using Jquery Code to fetch the data from PHP.</p>
<pre class="prettyprint" style="overflow:auto">&lt;script type="text/javascript"&gt;
$(function()
{
  $('#charsetdiv').load("test2.php");
});
&lt;/script&gt;
&lt;div id="charsetdiv"&gt;&lt;/div&gt;</pre>
<p>If you&#8217;ve not used jQuery then you might be wondering about the ajax code, you can check the <a href="http://roshanbh.com.np/2008/03/jquery-benefits-examples-free-ebook.html">benefits of jquery</a> from this post and download <a href="http://roshanbh.com.np/2008/03/jquery-benefits-examples-free-ebook.html">free ebook of jQuery</a>.</p>
<p>After getting value from Ajax, the division with id &#8220;charsetdiv&#8221;, let&#8217;s look at the output.</p>
<pre class="prettyprint" style="overflow:auto">?-accentu?e</pre>
<h4>Solving western Character Set problem with Ajax and PHP</h4>
<p style="text-align: justify;">The above problem is occurred because of not defining proper charset to these European characters. We can solve this problem by sending the header which define the appropriate Character Set for these characters.Let&#8217;s see, how I solve this problem just adding the header which define the charset Windows-1256 &#8211; which support Arabic as well as European characters.</p>
<pre class="prettyprint" style="overflow:auto">&lt;?php
  header("Content-Type: text/html; charset=Windows-1256\n");
  $str="€-accentuée";
  echo $str;
?&gt;</pre>
<p>After adding that header in PHP, you&#8217;ll get the appropriate european characters in  your Ajax enabled application.</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=276&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/09/solving-european-characters-western-charset-problem-with-ajax-and-php.html/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Changing textbox value from dropdown list using Ajax and PHP</title>
		<link>http://roshanbh.com.np/2008/09/change-textbox-value-dropdown-list-ajax-php.html</link>
		<comments>http://roshanbh.com.np/2008/09/change-textbox-value-dropdown-list-ajax-php.html#comments</comments>
		<pubDate>Thu, 04 Sep 2008 19:10:00 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/?p=253</guid>
		<description><![CDATA[Yesterday Tarquin macey asked me how can we change the value of the textbox based using Ajax and PHP based on the changing value of the dropdown list.Today, I&#8217;ve come up with a solution from him.In this post, you&#8217;ll see how to get the currency code of a country from PHP using Ajax and this [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F09%2Fchange-textbox-value-dropdown-list-ajax-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F09%2Fchange-textbox-value-dropdown-list-ajax-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: justify;">Yesterday   <a href="http://www.tarquinm.com/" target="_blank">Tarquin macey</a> asked me how can we change the value of the textbox based using Ajax and PHP based on the changing value of the dropdown list.Today, I&#8217;ve come up with a solution from him.In this post, you&#8217;ll see how to get the currency code of a country from PHP using Ajax and this currency code will replace the value of textbox each time the dropdown list changes.</p>
<p><span id="more-253"></span></p>
<p><strong><a href="http://roshanbh.com.np/examples/dropdown-textbox-ajax" target="_blank">View Live Demo</a> </strong></p>
<h4>Writing Code for changing textbox value from dropdown list using Ajax and PHP</h4>
<p>After looking at the above demo, let&#8217;s start writing code for the changing the currency code value in the textbox form Ajax using PHP when you changes the country from the dropdown list.</p>
<p><strong>HTML Code</strong></p>
<pre class="prettyprint" style="overflow:auto">&lt;select name="country" onChange="getCurrencyCode('find_ccode.php?country='+this.value)"&gt;
 &lt;option value=""&gt;Select Country&lt;/option&gt;
	&lt;option value="1"&gt;USA&lt;/option&gt;
	&lt;option value="2"&gt;UK&lt;/option&gt;
	&lt;option value="3"&gt;Nepal&lt;/option&gt;
&lt;/select&gt;
&lt;input type="text" name="cur_code" id="cur_code" &gt;</pre>
<p>As you can in the above code, there are two main components one dropdown list whose name is country and contains the list country in it.The JavaScript function <span style="color: #000080;"><span class="prettyprint" style="overflow:auto">getCurrencyCode</span>()</span> is called when user change value in the list. Note down the name and id of textbox which will have the currency code fetched from Ajax.</p>
<p><strong>JavaScript Code for changing textbox value without refreshing the page<br />
</strong></p>
<pre class="prettyprint" style="overflow:auto">function getCurrencyCode(strURL)
{
  var req = getXMLHTTP();
  if (req)
  {
	//function to be called when state is changed
	req.onreadystatechange = function()
	{
  	  //when state is completed i.e 4
	  if (req.readyState == 4)
	  {
		// only if http status is "OK"
		if (req.status == 200)
		{
			document.getElementById('cur_code').value=req.responseText;
		}
		else
		{
			alert("There was a problem while using XMLHTTP:\n" + req.statusText);
		}
	  }
        }
	req.open("GET", strURL, true);
	req.send(null);
  }
}</pre>
<p style="text-align: justify;">In the first line of the above code, the XMLHTTPRequest object is created using getXMLHTTP() function. To look at the the structure of this function, take a look at this old post <a href="http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html">change the value of dropdown list using Ajax and PHP</a> . After checking the appropriate value of <span style="color: #000080;">readystate</span>(4 means completed) and <span style="color: #000080;">status</span>(200 means ok) property of XMLHTTPRequest object, the value of textbox is replaced with the returned value from PHP using Ajax . The response ,which is can be accessed via <span class="prettyprint" style="overflow:auto">req.responseText property, is written to textbox via the value property of textbox. </span></p>
<p><strong>PHP code for changing the value of textbox using Ajax and PHP </strong></p>
<pre class="prettyprint" style="overflow:auto">&lt;?php
$country=$_REQUEST['country'];
switch($country)
{
	case "1" :
		echo "USD";
		break;
	case "2" :
		echo "GBP";
		break;
	case "3" :
		echo "NPR";
		break;
}
?&gt;</pre>
<p style="text-align: justify;">As you can see the above PHP code, it is fairly simple which just print the currency code value according to the name of country. The value which is in the echo statement is going to returned from PHP via Ajax.</p>
<p><strong><a href="http://roshanbh.com.np/codes/dropdown-textbox-ajax.zip" target="_blank">Download Full Source Code </a></strong></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=253&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/09/change-textbox-value-dropdown-list-ajax-php.html/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>jQuery is the most popular JavaScript and Ajax Framework</title>
		<link>http://roshanbh.com.np/2008/07/jquery-most-popular-javascript-ajax-framework.html</link>
		<comments>http://roshanbh.com.np/2008/07/jquery-most-popular-javascript-ajax-framework.html#comments</comments>
		<pubDate>Tue, 29 Jul 2008 17:54:59 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Poll & Competetion]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/?p=187</guid>
		<description><![CDATA[In last poll, Many people supported Zend Framework as the first choice for the PHP developments. And, today I would like to announce the result of the poll &#8220;which is the best JavaScript and Ajax framework ?&#8221; I would like to thanks all of you 690 voters who has participated in this poll. Today, I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F07%2Fjquery-most-popular-javascript-ajax-framework.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F07%2Fjquery-most-popular-javascript-ajax-framework.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: justify;">In last poll, Many people supported <a href="http://http://roshanbh.com.np/2008/06/zend-framework-most-popular-php-framework.html">Zend Framework as the first choice for the PHP developments</a>. And, today I would like to announce the result of the poll &#8220;which is the <a href="http://roshanbh.com.np/2008/06/which-is-the-best-javascript-ajax-framework.html">best JavaScript and Ajax framework</a> ?&#8221; I would like to thanks all of you 690 voters who has participated in this poll. Today, I&#8217;ve no hesitation on saying that jQuery is the most popular and leading choice of web developer as a JavaScript and Ajax framework.</p>
<p><span id="more-187"></span></p>
<p style="text-align: justify;">You can look at the poll result below. There is no dispute that  <a href="http://ejohn.org/" target="_blank">John Resig&#8217;s</a> JavaScript library (jQuery) is their first choice as 52% web developers have voted for this library. In this blog, I&#8217;ve also posted many jQuery tutorials, <a href="http://roshanbh.com.np/category/jquery">Check them out</a> .</p>
<p>[poll id="2"]</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=187&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/07/jquery-most-popular-javascript-ajax-framework.html/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Which is the best JavaScript and Ajax Framework ?</title>
		<link>http://roshanbh.com.np/2008/06/which-is-the-best-javascript-ajax-framework.html</link>
		<comments>http://roshanbh.com.np/2008/06/which-is-the-best-javascript-ajax-framework.html#comments</comments>
		<pubDate>Thu, 26 Jun 2008 09:05:26 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Poll & Competetion]]></category>
		<category><![CDATA[ajax framework]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/?p=137</guid>
		<description><![CDATA[Last time, I had published a poll asking which is the best PHP framework and in that poll Zend Framework stand out itself to be the most popular PHP framework among the PHP developers. This time ,I&#8217;ve come up with another Poll and I would you to participate in this POLL &#8220;Which is the best [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F06%2Fwhich-is-the-best-javascript-ajax-framework.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F06%2Fwhich-is-the-best-javascript-ajax-framework.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: justify;">Last time, I had published a poll asking which is the <a title="Best PHP Framework" href="http://roshanbh.com.np/2008/06/which-is-the-best-php-framework.html">best PHP framework</a> and in that poll Zend Framework stand out itself to be the most <a href="http://roshanbh.com.np/2008/06/zend-framework-most-popular-php-framework.html">popular PHP framework</a> among the PHP developers. This time ,I&#8217;ve come up with another Poll and I would you to participate in this POLL &#8220;Which is the best JavaScript and Ajax Framework?&#8221;.</p>
<p><span id="more-137"></span></p>
<p style="text-align: justify;">There are many JavaScript and Ajax framework available in the web and I think a lot of Web developers are confused which one to begin with. Today, I would like to hear from you which is the best JavaScript Framework among the popular JavaScript framework <a href="http://jquery.com" target="_blank">jquery</a>, <a href="http://prototypejs.org/" target="_blank">prototype.js</a>, <a href="http://mootools.net/" target="_blank">mootools</a>, <a href="http://dojotoolkit.org/" target="_blank">Dojo</a> and <a href="http://developer.yahoo.com/yui/" target="_blank">YUI</a>.</p>
<p style="text-align: justify;">First let&#8217;s see some facts about these framework.If you look at the <a href="http://www.google.com/trends?q=prototype+%2C+jquery%2C+YUI%2C+dojo%2C+mootools&amp;ctab=0&amp;geo=all&amp;date=all&amp;sort=0" target="_blank">google trend</a> of these JavaScript and Ajax framework Dojo seems to be dominant in the year 2007 but jQuery seems to be popular these days.</p>
<p style="text-align: justify;">And if you look at this <a href="http://mootools.net/slickspeed/" target="_blank">Slick speed selecter test</a> , it seems that Mootools is providing better performance than the other JavaScript frameworks. Prototype.js is one of the oldest JavaScript and Ajax framework which is widely used in many high profile websites over the Internet. Furthermore, YUI which is supported by yahoo and is another strong framework in the list.</p>
<p style="text-align: justify;">If you ask me which one is best JavaScript framework?, My personal favorite is jQuery as you can  see lots of examples in this blog with jQuery. One plus point of jQuery is that it is supported by large community and have a very good <a href="http://docs.jquery.com/Main_Page" target="_blank">documentaion with example in jquery.com</a>.</p>
<p style="text-align: justify;">But I would like to hear from you which JavaScript and Ajax Framework you recommend for other to  use in their project.</p>
<p>[poll id="2"]</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=137&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/06/which-is-the-best-javascript-ajax-framework.html/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Ajax login validation system in PHP using jQuery</title>
		<link>http://roshanbh.com.np/2008/04/ajax-login-validation-php-jquery.html</link>
		<comments>http://roshanbh.com.np/2008/04/ajax-login-validation-php-jquery.html#comments</comments>
		<pubDate>Thu, 24 Apr 2008 10:57:04 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/04/ajax-login-validation-php-jquery.html</guid>
		<description><![CDATA[Last time, I&#8217;ve showed you how to check user availability in Ajax using jQuery&#8217;s fading effect. But I&#8217;ve just shown the example without connecting the database and some people have faced problem with database connecting solution of that problem.In this post, I&#8217;ll show you how to use Ajax login system in php using jQuery and [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F04%2Fajax-login-validation-php-jquery.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F04%2Fajax-login-validation-php-jquery.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p align="justify">Last time, I&#8217;ve showed you how to <a href="http://roshanbh.com.np/2008/04/check-username-available-ajax-php-jquery.html">check user availability in Ajax using jQuery&#8217;s fading effect</a>. But I&#8217;ve just shown the example without connecting the database and some people have faced problem with database connecting solution of that problem.In this post, I&#8217;ll show you how to use Ajax login system in php using jQuery and some animation as well.</p>
<p><span id="more-98"></span></p>
<p align="justify"><strong><a href="http://roshanbh.com.np/examples/ajax-login-php" target="_blank">View Demo </a></strong></p>
<p align="justify">In this example, first of all we&#8217;ll validate the user login detail from ajax showing the messages with some animation. If authenticated, the user will be redirected to the secure page &#8220;secure.php&#8221;. If you&#8217;ll try to directly access &#8220;secure.php&#8221;, you can&#8217;t do that.</p>
<p align="justify">Now let&#8217;s look at the code how to do this,</p>
<p align="justify"><strong>HTML Code</strong></p>
<pre class="prettyprint" style="overflow: auto">&lt; form method="post" action="" id="login_form" /&gt;
  &lt; input name="username" type="text" id="username" value="" maxlength="20" /&gt;
  &lt; input name="password" type="password" id="password" value="" maxlength="20" /&gt;
  &lt; input name="Submit" type="submit" id="submit" value="Login" /&gt;
&lt; /form &gt;</pre>
<p align="justify">As you can see in html code, we&#8217;ve created the form with id &#8220;login_form&#8221;.</p>
<p align="justify">And furthermore, the CSS code is same as the one available in the pervious post of checking user availability in ajax and php.</p>
<h4>JavaScript Code for Ajax Login validation system in PHP</h4>
<p align="justify">First of all we need to use the jQuery library in our code,</p>
<pre class="prettyprint" style="overflow: auto">&lt; script src="jquery.js" type="text/javascript" language="javascript"&gt;&lt;/script &gt;</pre>
<p align="justify">Now let&#8217;s look at the code in javaScript to call ajax and show the animated message with fading effects.</p>
<pre class="prettyprint" style="overflow: auto">$("#login_form").submit(function()
{
	//remove all the class add the messagebox classes and start fading
	$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
	//check the username exists or not from ajax
	$.post("ajax_login.php",{ user_name:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {
	  if(data=='yes') //if correct login detail
	  {
		$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
		{
 		  //add message and change the class of the box and start fading
		  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
                  function()
		  {
  	  	     //redirect to secure page
		     document.location='secure.php';
		  });
		});
	  }
	  else
	  {
	  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
		{
		  //add message and change the class of the box and start fading
		  $(this).html('Your login detail sucks...').addClass('messageboxerror').fadeTo(900,1);
		});
          }
       });
       return false;//not to post the  form physically
});</pre>
<p align="justify">As you can see above this code is preety much similar to the previous post of <a href="http://roshanbh.com.np/2008/04/check-username-available-ajax-php-jquery.html">checking username availability in ajax and php</a> and you can see the explanation of the above code from that post. But in above code, where the user is validated, he&#8217;ll be logged into the &#8220;secure.php&#8221; using &#8220;document.location&#8221; in JavaScript.</p>
<pre class="prettyprint" style="overflow: auto">$("#password").blur(function()
{
	$("#login_form").trigger('submit');
});</pre>
<p align="justify">Well, as you can see above javaScript&#8217;s code, when focus is moved away from the password it also call the for sumit action. Basically whenever you hit tab button in password field, it starts validating the user detail using ajax.</p>
<h4>PHP Code for Ajax Login validation system</h4>
<p align="justify">First of all lets&#8217;s look at the code of the &#8220;ajax_login.php&#8221;.</p>
<pre class="prettyprint" style="overflow: auto">//get the posted values
$user_name=htmlspecialchars($_POST['user_name'],ENT_QUOTES);
$pass=md5($_POST['password']); 

//now validating the username and password
$sql="SELECT user_name, password FROM tbl_user WHERE user_name='".$user_name."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result); 

//if username exists
if(mysql_num_rows($result)&gt;0)
{
	//compare the password
	if(strcmp($row['password'],$pass)==0)
	{
		echo "yes";
		//now set the session from here if needed
		$_SESSION['u_name']=$user_name;
	}
	else
		echo "no";
}
else
	echo "no"; //Invalid Login</pre>
<p align="justify">As you can see above, the user login detial is validated from the database. If the user login detail doesn&#8217;t exists, it just returns the &#8220;no&#8221; values and if the user login detial does exists the it just return &#8220;yes&#8221; values with setting username in session variable.</p>
<p align="justify">Finally,  let&#8217;s look at the code of <strong>secure.php</strong></p>
<pre class="prettyprint" style="overflow: auto">if(empty($_SESSION['u_name']))
  header("Location:index.html");
//if logout then destroy the session and redirect the user
if(isset($_GET['logout']))
{
  session_destroy();
  header("Location:index.html");
}
echo " &lt;a href='secure.php?logout'&gt;&lt;b&gt;Logout&lt;b&gt;&lt;/a&gt; ";
echo " &lt;div align='center'&gt;You Are inside secured Page&lt;/a&gt; ";</pre>
<p align="justify">As you can see above the code of &#8220;secure.php&#8221; is simpleforward. If the user is not authenticated by session then he&#8217;ll be redirected to &#8220;index.html&#8221;. And there is link for &#8220;logout&#8221; in this page form where user can destroy the seession.</p>
<p align="justify"><strong><a href="http://roshanbh.com.np/codes/ajax-login-php.zip" target="_blank">Download Full Source Code</a> </strong></p>
<p align="justify"><strong>Implementation Guide:</strong></p>
<p align="justify">To implement this code, dump the tbl_user &#8220;table&#8221; available in the zip file to your database and configure the database connection in &#8220;ajax_login.php&#8221;.</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=98&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/04/ajax-login-validation-php-jquery.html/feed</wfw:commentRss>
		<slash:comments>156</slash:comments>
		</item>
		<item>
		<title>check username availability in ajax and php using jquery&#8217;s fading effect</title>
		<link>http://roshanbh.com.np/2008/04/check-username-available-ajax-php-jquery.html</link>
		<comments>http://roshanbh.com.np/2008/04/check-username-available-ajax-php-jquery.html#comments</comments>
		<pubDate>Sat, 05 Apr 2008 16:53:45 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[username]]></category>
		<category><![CDATA[username availability]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/04/check-username-available-ajax-php-jquery.html</guid>
		<description><![CDATA[In this post,I&#8217;ll show you how to make a fancy username availability checking in ajax and php using jquery.When, you check the the username avaiability a fancy message box will show you the message with a little bit of animation. If you are looking for such kind of effect for checking username availability, then this [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F04%2Fcheck-username-available-ajax-php-jquery.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F04%2Fcheck-username-available-ajax-php-jquery.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In this post,I&#8217;ll show you how to make a fancy username availability checking in ajax and php using jquery.When, you check the the username avaiability a fancy message box will show you the message with a little bit of animation. If you are looking for such kind of effect for checking username availability, then this might be right post for you.<br />
<span id="more-82"></span></p>
<p><a href="http://roshanbh.com.np/examples/username-available/" target="_blank"><strong><br />
View Live Demo</strong></a></p>
<p align="justify">Now let&#8217;s check it how to do check the username avaiability in ajax and php using jQuery.</p>
<p align="justify"><strong>Html Code :</strong></p>
<pre class="prettyprint" style="overflow: auto">&lt;div &gt;
 User Name : &lt;input name="username" type="text" id="username" value="" maxlength="15" /&gt;
 &lt;span id="msgbox" style="display:none"&gt;&lt;/span&gt;
&lt;/div&gt;</pre>
<p align="justify">As you can see the above the &#8220;span&#8221; with id &#8220;msgbox&#8221; will show you the username availability message from ajax.</p>
<p align="justify"><strong>Css code :</strong></p>
<pre style="border: 1px solid #888888; padding: 2px"><span style="color: #ff00ff;">.messagebox{</span>
<span style="color: #333399;"> position:absolute;
 width:100px;
 margin-left:30px;
 border:1px solid #c93;
 background:#ffc;
 padding:3px;</span>
<span style="color: #ff00ff;">}</span>
<span style="color: #ff00ff;">.messageboxok{</span>
<span style="color: #333399;"> position:absolute;
 width:auto;
 margin-left:30px;
 border:1px solid #349534;
 background:#C9FFCA;
 padding:3px;
 font-weight:bold;
 color:#008000;</span>
<span style="color: #ff00ff;">}</span>
<span style="color: #ff00ff;">.messageboxerror{</span>
<span style="color: #333399;"> position:absolute;
 width:auto;
 margin-left:30px;
 border:1px solid #CC0000;
 background:#F7CBCA;
 padding:3px;
 font-weight:bold;
 color:#CC0000;</span>
<span style="color: #ff00ff;">}</span></pre>
<p align="justify">I&#8217;ve defined three different class for three type of different message class &#8220;messagebox&#8221; for &#8220;checking&#8230;.&#8221; message, &#8220;messageboxok&#8221; and &#8220;messageboxerror&#8221; class for displaying username available and not available messages.</p>
<p align="justify">As you know you can change the attriubutes of the css of the above code but keep in mind that &#8220;position&#8221; property should be &#8220;absolute&#8221;.</p>
<p align="justify"><strong>Javascript code :</strong></p>
<p align="justify">First of  all, the jQuery library is used,</p>
<pre class="prettyprint" style="overflow: auto">&lt;script src="jquery.js" type="text/javascript" language="javascript"&gt;&lt;/script&gt;</pre>
<p align="justify">And when the focus is moved from the textbox of username the following function is called which call &#8220;user_availability.php&#8221; through ajax and display the appropriate message with fading effect.</p>
<pre class="prettyprint" style="overflow: auto">$("#username").blur(function()
{
 //remove all the class add the messagebox classes and start fading
 $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
 //check the username exists or not from ajax
 $.post("user_availability.php",{ user_name:$(this).val() } ,function(data)
 {
  if(data=='no') //if username not avaiable
  {
   $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
   {
    //add message and change the class of the box and start fading
    $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
   });
  }
  else
  {
   $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
   {
    //add message and change the class of the box and start fading
    $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
   });
  }
 });
});</pre>
<p align="justify">As you can see in the first line, &#8220;all&#8221; css class is removed from the div displaying the message and then &#8220;messagebox&#8221; class is added to that that element with adding the text &#8220;checking&#8221; within the element and displaying with fading effect.</p>
<p align="justify">After that, ajax is used to call the PHP file, and when response is received through Ajax then jQuery is used to show the respective message-box with fading effects.</p>
<p align="justify"><strong>Php Code:</strong></p>
<pre class="prettyprint" style="overflow: auto">//this varible contains the array of existing users
$existing_users=array('roshan','mike','jason');
//value got from the get metho
$user_name=$_POST['user_name'];
//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{
 //user name is not available
 echo "no";
}
else
{
  //username available i.e. user name doesn't exists in array
  echo "yes";
}</pre>
<p align="justify">In the above PHP code, I&#8217;ve added three usernames in a array and then check weather that user exists or not in that array and  print &#8220;yes&#8221; or &#8220;no&#8221; accordingly. The response taken from ajax is used within JavaScript function to  display the appropriate message.But, you can use database connection to check the the availability of username in your code.</p>
<p><strong><a href="http://roshanbh.com.np/codes/username-available.zip" target="_blank">Download full source</a></strong></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=82&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/04/check-username-available-ajax-php-jquery.html/feed</wfw:commentRss>
		<slash:comments>156</slash:comments>
		</item>
		<item>
		<title>How to call php from ajax in every second using Jquery</title>
		<link>http://roshanbh.com.np/2008/03/call-php-ajax-every-second-jquery.html</link>
		<comments>http://roshanbh.com.np/2008/03/call-php-ajax-every-second-jquery.html#comments</comments>
		<pubDate>Thu, 27 Mar 2008 17:58:01 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[call ajax]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/03/call-php-ajax-every-second-jquery.html</guid>
		<description><![CDATA[Few days ago, Mike emailed me and asked me how can i call a server script( php file) from jQuery in every second.Since, there was no built-in mechanism for this in jQuery, I came up with this solution of displaying time of server using Ajax, PHP and jQuery.You can use setinterval() method avaiable in javaScript [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F03%2Fcall-php-ajax-every-second-jquery.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F03%2Fcall-php-ajax-every-second-jquery.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p align="justify">Few days ago, Mike emailed me and asked me how can i call a server script( php file) from jQuery in every second.Since, there was no built-in mechanism for this in jQuery, I came up with this solution of displaying time of server using Ajax, PHP and jQuery.You can use setinterval() method avaiable in javaScript to accomplish this task using jQuery.</p>
<p><span id="more-71"></span><br />
<strong><a href="http://roshanbh.com.np/examples/clock-server-time/" target="_blank">View Live Demo</a></strong><br />
<strong><br />
HTML Code:</strong></p>
<pre class="prettyprint" style="overflow: auto">&lt;div align="center" id="timeval"&gt;--:--:--&lt;/div&gt;
&lt;button id="stop"&gt;Stop&lt;/button&gt;</pre>
<p align="justify">There are mainly two elements one is &#8220;div&#8221; with id &#8220;timeval&#8221; which displays the the time and the other one is &#8220;button&#8221; with id &#8220;stop&#8221; to stop the calling the PHP file in the regular interval.</p>
<p><strong>JavaScript Code:</strong></p>
<pre class="prettyprint" style="overflow: auto">&lt;script src="jquery.js"&gt;&lt;/script&gt;
&lt;script&gt;
$(document).ready(function()
{
   //ajaxTime.php is called every second to get time from server
   var refreshId = setInterval(function()
   {
     $('#timeval').load('ajaxTime.php?randval='+ Math.random());
   }, 1000);

   //stop the clock when this button is clicked
   $("#stop").click(function()
   {
     clearInterval(refreshId);
   });
});
&lt;/script&gt;</pre>
<p align="justify">As you can see above, setInterval() function is used to call the php file in every second, where 1000 means 1000 millisecond which equals to one second.And, the load() function of jQuery is used to call the Ajax. And, you might be wondering why I&#8217;ve passed &#8220;randval&#8221; to &#8220;ajaxTime.php&#8221;, you can <a href="http://roshanbh.com.np/2008/01/how-to-solve-the-problem-of-retrieving-same-value-by-ajax-browser-cache-problem.html" target="_blank">read this post of mine about the problem of getting same value from ajax</a>. And when the button with id &#8220;stop&#8221; is called the clearInterval() functions clears the interval  ID generated by setInterval()  and  stop calling the PHP file at regular interval.</p>
<p><strong>PHP code :</strong></p>
<pre class="prettyprint" style="overflow: auto">&lt;?php
   echo date("g:i:s A");
?&gt;</pre>
<p align="justify">As you can see the php code in the ajaxTime.php is fairly simple, it just displays the current server time.</p>
<p><a href="http://roshanbh.com.np/codes/call-php-every-second-jquery.zip" target="_blank"> You can download the demo by clicking here</a></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=71&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/03/call-php-ajax-every-second-jquery.html/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Jquery : Benefits, Examples and Free Ebook</title>
		<link>http://roshanbh.com.np/2008/03/jquery-benefits-examples-free-ebook.html</link>
		<comments>http://roshanbh.com.np/2008/03/jquery-benefits-examples-free-ebook.html#comments</comments>
		<pubDate>Thu, 13 Mar 2008 11:18:54 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[e-book]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/03/jquery-benefits-examples-free-ebook.html</guid>
		<description><![CDATA[What is jQuery? Directly taken from the website of jQuery -&#8221; jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages&#8221;.It is basically a JavaScript framework for writing huge task of JavaScript with few lines of code. What are [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F03%2Fjquery-benefits-examples-free-ebook.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F03%2Fjquery-benefits-examples-free-ebook.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h4>What is jQuery?</h4>
<p align="justify">Directly taken from the website of <a title="jquery" href="http://jquery.com" target="_blank">jQuery</a> -&#8221; jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages&#8221;.It is basically a JavaScript framework for writing huge task of JavaScript with few lines of code.</p>
<p><span id="more-38"></span></p>
<h4>What are the benefits of jQuery?</h4>
<p align="justify"><strong>1) </strong>5 lines of code of jQuery can perform better task than 25 lines of traditional Javascript code.<br />
<strong> 2) </strong>Various number of plugins are available and a large community support this project it is better to use jQuery in your project compared to other JavaScript frameworks like <a href="http://www.prototypejs.org" target="_blank">prototype.js</a> or <a href="http://mootools.net/" target="_blank">moottools</a> .<br />
<strong> 3) </strong>jQuery&#8217;s website contains <a title="jquery documentation" href="http://docs.jquery.com/Main_Page" target="_blank">very good documentation with useful examples</a>.<br />
<strong> 4) </strong>It simplifies the Ajax Development. Just have to write 5 or 6 lines to call the Ajax. Isn&#8217;t that easy??</p>
<p align="justify">Well these are just few benefits of jQuery but if you start using jQuery then you&#8217;ll come  to know about the actual benefits of using jQuery. And believe me, It worth using jQuery in your web applications otherwise just blame me.</p>
<h4>Basic Examples of jQuery :</h4>
<pre class="prettyprint">$(document).ready(function()
{
    $(".button").click(function ()
    {
      $("p").hide("slow");
    });
});</pre>
<p>In the above example,  when a element with class &#8220;button&#8221; is clicked, all the paragraph element (&#8220;p&#8221;) will be hidden in the whole document in slow movement i.e. with slight animation.</p>
<pre class="prettyprint">$(document).ready(function()
{
      $("#divid").load("sample.php");
});</pre>
<p>Above is the example Ajax using jQuery, look how simple it is to use ajax using jquery.The response got from the sample.php file is filled inside the element whose id is &#8220;divid&#8221;.</p>
<h4>Free Ebook for download :</h4>
<p align="justify">Well I&#8217;ve downloaded a E-book of jQuery in chm format.It contains chm version of the document found in docs.jquery.com. This E-book contains the very good documentation as well as useful examples.</p>
<p>You can <a title="jquery ebook" href="http://roshanbh.com.np/ebooks/jQuery-1.2-v1.zip" target="_blank">download the Ebook by clicking here.</a></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=38&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/03/jquery-benefits-examples-free-ebook.html/feed</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>How to return value from Ajax Function &#8211; Use synchronous request</title>
		<link>http://roshanbh.com.np/2008/01/how-to-return-value-from-ajax-function-use-synchronous-request.html</link>
		<comments>http://roshanbh.com.np/2008/01/how-to-return-value-from-ajax-function-use-synchronous-request.html#comments</comments>
		<pubDate>Tue, 29 Jan 2008 18:42:35 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[coding technique]]></category>
		<category><![CDATA[tips and technique]]></category>
		<category><![CDATA[ajax requests]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/01/how-to-return-value-from-ajax-function-use-synchronous-request.html</guid>
		<description><![CDATA[Have you ever tried to return values from the Ajax function ? Well it might be sometime the cases when you might have to return the value got from the server to another function rather than updating the component. Let&#8217;s try to return the values from the traditional Ajax function. &#160; &#160; function getVal(param) { [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F01%2Fhow-to-return-value-from-ajax-function-use-synchronous-request.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F01%2Fhow-to-return-value-from-ajax-function-use-synchronous-request.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p align="justify">Have you ever tried to return values from the Ajax function ? Well it might be sometime the cases when you might have to return the value got from the server to another function rather than updating the component.</p>
<p>Let&#8217;s try to return the values from the traditional Ajax function.<br />
<span id="more-37"></span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre style="overflow: auto"><font color="#800000">function getVal(param)
{
  var strURL=”findValues.php?val=”+param;
  var req = getXMLHTTP();
  if (req)
  {
    req.onreadystatechange = function()
    {
      if (req.readyState == 4)
      {
        return req.responseText;
      }
    }

    req.open(”GET”, strURL, true);
    req.send(null);
  }
}</font></pre>
<p>And now look at the following statement ,</p>
<p><font color="#800000">alert(getVal(1));</font></p>
<p><!--adsense--></p>
<p align="justify">What does the alert statement displays? Is it the value returned from the server?? Not at all, it alert &#8220;undefined&#8221;. Why so?? What happens here is you are trying to get the synchronous request but the request runs asynchronously.So to return the value, we need to make the request synchronous so that we can return the value from the Ajax function.</p>
<p align="justify">So what is the difference between synchronous request and asynchronous request of Ajax?? In the above example which is asynchronous request, the callback function is called in the background leaving the other activity of your program to flow no matter weather the result is obtained from the server or not. But in the case of synchronous request, JavaScript stop processing your program  until a result has been obtained from the server.</p>
<p>Now let&#8217;s look at the synchronous way of request of the above function</p>
<pre style="overflow: auto"><font color="#800000">
function getVal(param)
{
  var strURL=”findValues.php?val=”+param;
  var req = getXMLHTTP();
  req.open(”GET”, strURL, false); //third parameter is set to false here
  req.send(null);
  return req.responseText;
}</font></pre>
<p>If you look at the open method, the third parameter is false which means the the request should be synchronous, if set to true the request become asynchronous and the callback function and onreadystatechange property must be used.</p>
<p><a href="http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX" title="Ajax Synchronous and asynchronous" target="_blank">Click here to learn more about synchronous request</a>.</p>
<p><!--adsense--></p>
<p align="justify"><strong>Note:You might face the problem of browser stuck, slow website link with the synchronous request of the Ajax so you have keep these things into account before making the synchronous request.</strong></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=37&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/01/how-to-return-value-from-ajax-function-use-synchronous-request.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to solve the problem of retrieving same value by Ajax &#8211; Browser Cache Problem</title>
		<link>http://roshanbh.com.np/2008/01/how-to-solve-the-problem-of-retrieving-same-value-by-ajax-browser-cache-problem.html</link>
		<comments>http://roshanbh.com.np/2008/01/how-to-solve-the-problem-of-retrieving-same-value-by-ajax-browser-cache-problem.html#comments</comments>
		<pubDate>Mon, 28 Jan 2008 11:24:53 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[tips and technique]]></category>
		<category><![CDATA[ajax problem]]></category>
		<category><![CDATA[same value ajax]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/01/how-to-solve-the-problem-of-retrieving-same-value-by-ajax-browser-cache-problem.html</guid>
		<description><![CDATA[You might have faced the problem of the getting same response on the consecutive request to a PHP file from Ajax. Well, what happens when making repeated GET requests to the same URL can often lead to the response coming not from the server but from the browser cache. This problem mainly occurs while using [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F01%2Fhow-to-solve-the-problem-of-retrieving-same-value-by-ajax-browser-cache-problem.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F01%2Fhow-to-solve-the-problem-of-retrieving-same-value-by-ajax-browser-cache-problem.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p align="justify">You might have faced the problem of the getting same response on the consecutive request to a PHP file from Ajax. Well, what happens when making repeated GET requests to the same  URL can often lead to the response coming not from the server but from the browser cache. This problem mainly occurs while using in internet explorer.</p>
<p>Now let&#8217;s look at the various methods to solve this problem :<br />
<span id="more-35"></span><br />
<strong>1) By Using setRequestHeader() method</strong></p>
<p>Well you can use the setRequestHeader() method of the XMLHttpRequest. This function adds a custom HTTP headers to the request.</p>
<p align="justify">Look at the example below  in which I&#8217;ve used &#8220;If-Modified-Since&#8221; header, this request header is used with get method to make it conditional i.e. if the requested resource has been modified since Sat, 1 Jan 2000 00:00:00 GMT, a copy of the resource will be returned from the server as the normal get request, it will not return from the browser&#8217;s cache.</p>
<p>Example Code:</p>
<pre style="overflow: auto"><span style="color: #800000;">request.open("GET", strURL, true);
request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); //add this line
request.send(null);</span></pre>
<p><strong>2) By passing a random element to URL in each get request</strong></p>
<p align="justify">The above method of sending header request might be difficult and even sometime it&#8217;s become ineffective as well. This is the method which always use and recommend you  as well.</p>
<p align="justify">An effective way of get rid this problem is to add a random element to the URL to which the repetitive request is sent. The browser interprets this as a request to a distinct page<br />
and returns a server page, not the cached version.</p>
<p><span style="color: #800000;">var url = &#8220;ajaxFile.php?randval=&#8221;+Math.random();</span></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=35&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/01/how-to-solve-the-problem-of-retrieving-same-value-by-ajax-browser-cache-problem.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Populate triple drop down list from database using Ajax and PHP</title>
		<link>http://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html</link>
		<comments>http://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html#comments</comments>
		<pubDate>Thu, 24 Jan 2008 10:17:35 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[drop down]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html</guid>
		<description><![CDATA[I&#8217;ve got many email from people asking for populating triple drop down list from the database without refreshing page using Ajax and PHP after posting the first article related to the ajax dropdown list using php .In this post, I&#8217;ve put three drop down of country , state and city and the drop down&#8217;s value [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F01%2Fpopulate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F01%2Fpopulate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I&#8217;ve got many email from people asking for populating triple drop down list from the database without refreshing page using Ajax and PHP after posting the first article related to the <a href="http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html" title="Ajax php dropdown list option change using php and ajax" target="_blank">ajax dropdown list using php </a>.In this post, I&#8217;ve put three drop down of country , state and city and the drop down&#8217;s value changes without refreshing the page.  Now let show you how to create it quickly.<span id="more-34"></span><br />
First create the following tables of country city and states,</p>
<pre style="overflow: auto"><font color="#800000">CREATE TABLE `country` (
  `id` tinyint(4) NOT NULL auto_increment,
  `country` varchar(20) NOT NULL default '',
PRIMARY KEY  (`id`)
) TYPE=MyISAM   ;

CREATE TABLE `state` (
 `id` tinyint(4) NOT NULL auto_increment,
 `countryid` tinyint(4) NOT NULL,
`statename` varchar(40) NOT NULL,
PRIMARY KEY  (`id`)
) TYPE=MyISAM   ;

CREATE TABLE `city` (
`id` tinyint(4) NOT NULL auto_increment,
`city` varchar(50) default NULL,
`stateid` tinyint(4) default NULL,
`countryid` tinyint(4) NOT NULL,
PRIMARY KEY  (`id`)
) TYPE=MyISAM   ;</font></pre>
<p>Now place the following form in the <strong>index.php</strong> file</p>
<pre style="overflow: auto"><font color="#800000">&lt;form method="post" name="form1"&gt;
 &lt;table border="0" cellpadding="0" cellspacing="0" width="60%"&gt;&lt;tbody&gt;
  &lt;tr&gt;
   &lt;td width="150"&gt;Country&lt;/td&gt;
   &lt;td width="150"&gt;&lt;select style="background-color: #ffffa0" name="country" onchange="getState(this.value)"&gt;&lt;option&gt;Select Country&lt;/option&gt;&lt;option value="1"&gt;USA&lt;/option&gt;&lt;option value="2"&gt;Canada&lt;/option&gt;       &lt;/select&gt;&lt;/td&gt;
  &lt;/tr&gt;
 &lt;tr&gt;
  &lt;td&gt;State&lt;/td&gt;
  &lt;td&gt;
  &lt;p id="statediv"&gt;
  &lt;select style="background-color: #ffffa0" name="state"&gt;&lt;option&gt;Select Country First&lt;/option&gt;       &lt;/select&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;City&lt;/td&gt;
  &lt;td&gt;
  &lt;p id="citydiv"&gt;
  &lt;select style="background-color: #ffffa0" name="city"&gt;&lt;option&gt;Select State First&lt;/option&gt;       &lt;/select&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/form&gt;</font></pre>
<p>As you can see above, in the onChage event of the country drop down getState() function of the javascript is called which change the options values the State drop down, let&#8217;s look at the code the getState() function.</p>
<pre style="overflow: auto"><font color="#800000">function getState(countryId)
{
   var strURL="findState.php?country="+countryId;
   var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
      if (req.readyState == 4)
      {
	 // only if "OK"
	 if (req.status == 200)
         {
	    document.getElementById('statediv').innerHTML=req.responseText;
	 } else {
   	   alert("There was a problem while using XMLHTTP:\n" + req.statusText);
	 }
       }
      }
   req.open("GET", strURL, true);
   req.send(null);
   }
}</font></pre>
<p>The code of the PHP file <strong>findState.php</strong>, which populate the options in the drop down of the state which is fetched from Ajax , is given below</p>
<pre style="overflow: auto"><font color="#800000">&lt;? $country=intval($_GET['country']);
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="SELECT id,statename FROM state WHERE countryid='$country'";
$result=mysql_query($query);

?&gt;
&lt;select name="state" onchange="getCity(&lt;?=$country?&gt;,this.value)"&gt;
 &lt;option&gt;Select State&lt;/option&gt;
  &lt;? while($row=mysql_fetch_array($result)) { ?&gt;
    &lt;option value=&lt;?=$row['id']?&gt;&gt;&lt;?=$row['statename']?&gt;&lt;/option&gt;
  &lt;? } ?&gt;
&lt;/select&gt;</font></pre>
<p>In the above state dropdown, getCity() function is called in onChage event with countryId and stateId parameter, now let&#8217;s look at the code of the getCity() function</p>
<pre style="overflow: auto"><font color="#800000">function getCity(countryId,stateId)
{
  var strURL="findCity.php?country="+countryId+"&amp;state="+stateId;
  var req = getXMLHTTP();
  if (req)
  {
    req.onreadystatechange = function()
    {
      if (req.readyState == 4) // only if "OK"
      {
        if (req.status == 200)
        {
          document.getElementById('citydiv').innerHTML=req.responseText;
        } else {
          alert("There was a problem while using XMLHTTP:\n" + req.statusText);
        }
      }
    }
    req.open("GET", strURL, true);
    req.send(null);
  }
}</font></pre>
<p>In the above ajax function, findcity.php is called and this PHP file populate the city dropdown according to the supplied parameters country and state from get method. Now let&#8217;s look at the code of <strong>findcity.php</strong>,</p>
<pre style="overflow: auto"><font color="#800000">&lt;?php $countryId=intval($_GET['country']);
$stateId=intval($_GET['state']);
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="SELECT id,city FROM city WHERE countryid='$countryId' AND stateid='$stateId'";
$result=mysql_query($query);

?&gt;
&lt;select name="city"&gt;
 &lt;option&gt;Select City&lt;/option&gt;
  &lt;?php while($row=mysql_fetch_array($result)) { ?&gt;
 &lt;option value&gt;&lt;?=$row['city']?&gt;&lt;/option&gt;
&lt;?php } ?&gt;
&lt;/select&gt;</font></pre>
<p>And thats all, the triple drop down list of city, country and state using Ajax and PHP will be  populated.<br />
<strong><a href="http://roshanbh.com.np/dropdown/" title="triple dropdown ajax php" target="_blank">VIew Live Demo</a></strong><br />
<strong><a href="http://www.roshanbh.com.np/dropdown/ajax_php_triple_dropdown.zip" title="source code of php ajax triple dropdown" target="_blank"> To download full source code, click here</a></strong></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=34&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html/feed</wfw:commentRss>
		<slash:comments>157</slash:comments>
		</item>
		<item>
		<title>Slider Using PHP, Ajax And Javascript</title>
		<link>http://roshanbh.com.np/2008/01/slider-using-php-ajax-and-javascript.html</link>
		<comments>http://roshanbh.com.np/2008/01/slider-using-php-ajax-and-javascript.html#comments</comments>
		<pubDate>Thu, 10 Jan 2008 19:01:03 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Slider]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/01/slider-using-php-ajax-and-javascript.html</guid>
		<description><![CDATA[Have you been searching for the slider script and implementing it in Ajax with PHP then you are in the right place buddy.Here is the post for you in which I&#8217;ve implemented javascript slider to work in Ajax using PHP. Ok let&#8217;s Begin it by creating the following tables in the database. Here is the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F01%2Fslider-using-php-ajax-and-javascript.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F01%2Fslider-using-php-ajax-and-javascript.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p align="justify">Have you been searching for the slider script and implementing it in Ajax with PHP then you are in the right place buddy.Here is the post for you in which I&#8217;ve implemented javascript slider to work in Ajax using PHP.  Ok let&#8217;s Begin it by creating the following tables in the database. Here is the mysql code to create the table named &#8220;tbl_slider&#8221; in database.</p>
<pre style="overflow: auto"><span style="color: #800000;">CREATE TABLE `tbl_slider` (
  `id` smallint(6) NOT NULL auto_increment,
  `slider_val` smallint(6) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM   ;</span></pre>
<p><span id="more-27"></span></p>
<p><a title="ajax , php and javascript code of slider " href="http://www.roshanbh.com.np/slider/" target="_blank"><strong>VIEW DEMO</strong></a><br />
<a title="Php ajax slider code" href="http://www.roshanbh.com.np/slider/slider_php_ajax_javascript.zip" target="_blank"> <strong>DOWNLOAD SOURCE CODE</strong></a></p>
<p align="justify">And now, there is a file slider.js which contains the code of javascript for the slider. I would like to say thanks to <a title="Slider using Javascript" href="http://carpe.ambiprospect.com/" target="_blank">Tom Hermansson Snickars</a> for the slider script. The main line you&#8217;ve notice in the slider.js is 164 where I&#8217;ve place the following code to call the Ajax function which is called when the slider is released after moving i.e on &#8220;onMouseUp&#8221; event.</p>
<pre style="overflow: auto"><span style="color: #800000;">setSliderVal(document.getElementById('slider1').style.left);</span></pre>
<p align="justify">Slider1 is the if Division(div) which is available in the index.php file. The following code displays the slider in the index.php file.</p>
<pre style="overflow: auto"><span style="color: #800000;">&lt;DIV class=carpe_slider_group&gt;
 &lt;DIV class=carpe_horizontal_slider_display_combo&gt;
  &lt;DIV class=carpe_slider_display_holder&gt;
   &lt;!-- Default value: 0 --&gt;
   &lt;input name="Input" class=carpe_slider_display id="display1" alue="&lt;?=$row['slider_val']?&gt;" /&gt;
  &lt;/DIV&gt;
 &lt;DIV class=carpe_horizontal_slider_track&gt;
 &lt;DIV class=carpe_slider_slit&gt;&lt;/DIV&gt;
 &lt;DIV class=carpe_slider id=slider1 display="display1" tyle="left:&lt;?=$row['slider_val']?&gt;px"&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class=carpe_horizontal_slider_display_combo&gt;&lt;/DIV&gt;
&lt;DIV class=carpe_horizontal_slider_display_combo&gt;&lt;/DIV&gt;
&lt;DIV class=carpe_horizontal_slider_display_combo&gt;&lt;/DIV&gt;
&lt;/DIV&gt;</span></pre>
<p align="justify">As you can see above there is &#8220;&lt;?=$row['slider_val']?&gt;&#8221; which is value of the slider stored in the database, the code for retrieving value from database look like this you can find this code in the index.php file</p>
<pre style="overflow: auto"><span style="color: #800000;">$link = mysql_connect('localhost', 'root', ''); //change the configuration if required
if (!$link) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_slider'); //change the name of the database if equired
$query="SELECT slider_val FROM  tbl_slider WHERE id='1'";
$result=mysql_query($query);
$row=mysql_fetch_array($result);</span></pre>
<p align="justify">And in the index.php there is Ajax function called &#8220;setSliderVal()&#8221; which is called from silder.js and use the ajax to call AJAX4Slider.php and retrive the current value of the slider from database.Here is the code of the AJAX4Slider.php</p>
<pre style="overflow: auto"><span style="color: #800000;">$sliderval=$_GET['sliderval']; //get the value from ajax function
$link = mysql_connect('localhost', 'root', ''); //change the onfiguration in required
if (!$link) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_slider'); //change the name of the database if required
$query="UPDATE tbl_slider SET slider_val='$sliderval' WHERE id='1'";
$result=mysql_query($query);</span></pre>
<p>The above code saves the value of the slider to the database.</p>
<p><a title="ajax , php and javascript code of slider " href="http://www.roshanbh.com.np/slider/" target="_blank"><strong>VIEW DEMO</strong></a><br />
<a title="Php ajax slider code" href="http://www.roshanbh.com.np/slider/slider_php_ajax_javascript.zip" target="_blank"> <strong>DOWNLOAD SOURCE CODE</strong></a></p>
<p><a rel="nofollow" href="http://www.testking.com/1Y0-259.htm"><strong><span style="color: #ff0000;">Security Note :</span></strong></a> It is just a simple demo and If you want to use this code in your professional project then there is a security issue in the PHP code, please use <span style="color: #333399;">$sliderval=intval($_GET['sliderval']);</span> in the php code instead of <span style="color: #333399;">$sliderval=$_GET['sliderval'];</span> in the AJAX4Slider.php to prevent your site from<a rel="nofollow" href="http://www.testking.com/1Y0-259.htm"> </a><a title="sql injection attack example and prevention" href="/2007/12/sql-injection-attack-examples-and-preventions-in-php.html" target="_blank">sql injection attack</a>.</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=27&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/01/slider-using-php-ajax-and-javascript.html/feed</wfw:commentRss>
		<slash:comments>68</slash:comments>
		</item>
		<item>
		<title>Change dropdown list (options) values from database with ajax and php</title>
		<link>http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html</link>
		<comments>http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html#comments</comments>
		<pubDate>Thu, 13 Dec 2007 17:58:00 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ajax dropdown]]></category>
		<category><![CDATA[php ajax dropdown]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/change-dropdown-list-options-values-from-database-with-ajax-and-php/</guid>
		<description><![CDATA[I&#8217;m going to show you a example in php and ajax to change the values of the dropdown&#8217;s options without refreshing the page. The values (options) of the dropdown are fetched from the database and the certain portion of the web pages is only refreshed without need to refresh the whole page. Let&#8217;s start with [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2007%2F12%2Fchange-dropdown-list-options-values-from-database-with-ajax-and-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2007%2F12%2Fchange-dropdown-list-options-values-from-database-with-ajax-and-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: justify">I&#8217;m going to show you a example in php and ajax to change the values of the dropdown&#8217;s options without refreshing the page. The values (options) of the dropdown are fetched from the database and the certain portion of the web pages is only refreshed without need to refresh the whole page.</p>
<p><span id="more-14"></span><br />
Let&#8217;s start with creating the two tables country and city and insert some data</p>
<blockquote style="color: #990000">
<pre style="overflow: auto">CREATE TABLE country
(
   id tinyint(4) NOT NULL auto_increment,
   country varchar(20) NOT NULL default '',
   PRIMARY KEY  (id)
) TYPE=MyISAM;CREATE TABLE city
(
  id tinyint(4) NOT NULL auto_increment,
  city varchar(50) default NULL,
  countryid tinyint(4) default NULL,
  PRIMARY KEY  (id)
) TYPE=MyISAM;</pre>
</blockquote>
<p>Now let&#8217;s look at the html code, let&#8217;s look at the code of the form and its elements</p>
<blockquote>
<pre style="overflow: auto"><span style="color: #990000;">&lt;form method="post" action="" name="form1"&gt;</span>
<span style="color: #990000;">Country : &lt;select name="country" nChange="getCity('findcity.php?country='+this.value)"&gt;</span>
<span style="color: #990000;"> &lt;option value=""&gt;Select Country&lt;/option&gt;</span>
<span style="color: #990000;"> &lt;option value="1"&gt;USA&lt;/option&gt;</span>
<span style="color: #990000;"> &lt;option value="2"&gt;Canada&lt;/option&gt;</span>
<span style="color: #990000;">     &lt;/select&gt;</span>
<span style="color: #990000;">&lt;br /&gt;</span><span style="color: #990000;">City : &lt;div id="citydiv"&gt;</span>
<span style="color: #990000;"> &lt;select name="select"&gt;</span>
<span style="color: #990000;"> &lt;option&gt;Select City&lt;/option&gt;</span>
<span style="color: #990000;">     &lt;/select&gt;</span>
<span style="color: #990000;"> &lt;/div&gt;</span>
<span style="color: #990000;">&lt;/form&gt;</span></pre>
</blockquote>
<p>As you can see that when the dropdown named &#8220;country&#8221; is changed the &#8220;getCity&#8221; function is called. Look at the other dropdown carefully, it is inside the division called &#8220;citydiv&#8221;.</p>
<p>Now let&#8217;s look at the javascript function called &#8220;getCity&#8221;</p>
<blockquote>
<pre style="overflow: auto"><span style="color: #990000;">function getCity(strURL)</span>
<span style="color: #990000;">{         </span>
<span style="color: #990000;"> var req = getXMLHTTP(); // fuction to get xmlhttp object</span>
<span style="color: #990000;"> if (req)</span>
<span style="color: #990000;"> {</span>
<span style="color: #990000;">  req.onreadystatechange = function()</span>
<span style="color: #990000;"> {</span>
<span style="color: #990000;">  if (req.readyState == 4) { //data is retrieved from server</span>
<span style="color: #990000;">   if (req.status == 200) { // which reprents ok status                    </span>
<span style="color: #990000;">     document.getElementById('citydiv').innerHTML=req.responseText;</span>
<span style="color: #990000;">  }
  else
  {</span> <span style="color: #990000;">
     alert("There was a problem while using XMLHTTP:\n");</span>
  <span style="color: #990000;">}</span>
  <span style="color: #990000;">}            </span>
<span style="color: #990000;">  }        </span>
<span style="color: #990000;">req.open("GET", strURL, true); //open url using get method</span>
<span style="color: #990000;">req.send(null);</span>
<span style="color: #990000;"> }</span><span style="color: #990000;">
}</span></pre>
</blockquote>
<p>now we&#8217;ve to create the file called findcity.php and put the following PHP code</p>
<blockquote>
<pre style="overflow: auto"><span style="color: #990000;">&lt;? $country=$_GET['country'];</span>
<span style="color: #990000;">$link = mysql_connect('localhost', 'root', ''); /change the </span><span style="color: #990000;">configuration if required</span>
<span style="color: #990000;">if (!$link) {</span>
  <span style="color: #990000;">die('Could not connect: ' . mysql_error());</span>
<span style="color: #990000;">}</span>
<span style="color: #990000;">mysql_select_db('db_ajax'); //change this if required</span>
<span style="color: #990000;">$query="select city from city where countryid=$country";</span>
<span style="color: #990000;">$result=mysql_query($query);</span><span style="color: #990000;">?&gt;</span>
<span style="color: #990000;">&lt;select name="city"&gt;</span>
<span style="color: #990000;">&lt;option&gt;Select City&lt;/option&gt;</span>
<span style="color: #990000;">&lt;? while($row=mysql_fetch_array($result)) { ?&gt;</span>
<span style="color: #990000;">   &lt;option value&gt;&lt;?=$row['city']?&gt;&lt;/option&gt;</span>
<span style="color: #990000;">&lt;? } ?&gt;</span>
<span style="color: #990000;">&lt;/select&gt;</span></pre>
</blockquote>
<p style="text-align: justify">Thats all, whenever you change the dropdown of country the values of the city dropdown is automaticalled changed without refreshing the page.<br />
If you want to download full source code, <a href="http://www.roshanbh.com.np/ajax_php_dropdown.zip"><strong>Click here to download</strong></a><strong>.</strong>
</p>
<p align="justify"><strong><span style="color: #ff0000;">Security Note :</span></strong> If you want to use this code in your project then there is a security flaw in the PHP code, please use <span style="color: #333399;">$country=intval($_GET['country']);</span> in the php code instead of <span style="color: #333399;">$country=$_GET['country'];</span> in the findcity.php to prevent your site from <a title="sql injection attack example and prevention" href="http://roshanbh.com.np/2007/12/sql-injection-attack-examples-and-preventions-in-php.html" target="_blank">sql injection attack</a>.</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=14&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html/feed</wfw:commentRss>
		<slash:comments>117</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/65 queries in 0.823 seconds using disk: basic
Object Caching 2048/2211 objects using disk: basic

Served from: roshanbh.com.np @ 2012-02-08 19:40:08 -->
