<?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>Utsav Singh Rathour </title>
	<atom:link href="http://rathour.com.np/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://rathour.com.np/blog</link>
	<description>Php,mysql,wordpress,asp.net,ajax,c#,vb.net, web designing, web development, website</description>
	<lastBuildDate>Mon, 08 Feb 2010 13:40:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ajax and PHP change the value of select menu with the change in textbox</title>
		<link>http://rathour.com.np/blog/ajax-and-php-change-the-value-of-select-menu-with-the-change-in-textbox/</link>
		<comments>http://rathour.com.np/blog/ajax-and-php-change-the-value-of-select-menu-with-the-change-in-textbox/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 13:24:56 +0000</pubDate>
		<dc:creator>Utsav</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://rathour.com.np/blog/?p=12</guid>
		<description><![CDATA[Today in this tutorial I shall teach you how to change the values of select menu with the change in textbox.  For example if we type “a” in the textbox than the select menu should show all the names starting from “a” without loading the page and continue as the change in textbox.
Ok so let [...]]]></description>
			<content:encoded><![CDATA[<p>Today in this tutorial I shall teach you how to change the values of select menu with the change in textbox.  For example if we type “a” in the textbox than the select menu should show all the names starting from “a” without loading the page and continue as the change in textbox.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Ok so let us start. First make the input form and call it “form.php”</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><code><br />
&lt;script language="javascript" type="text/javascript" src="search.js"&gt;&lt;/script&gt;</code></div>
<p><code></p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;form name='myForm' style='padding-top: 10px; padding-bottom: 7px;'&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Type Value here:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;input type='text' name='searchQuery' id='searchQuery' onkeyup="ajaxSearch();" /&gt;&amp;nbsp;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;/form&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;div id='ajaxDiv'&gt;&lt;?php include('search.php'); ?&gt;&lt;/div&gt;</div>
<p></code></p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In the above form we first link the JavaScript file that will have the action for our job. The next part we create a form with textbox and assign an action on to it on “onkeyup” event. This will do the action when any new letter is typed in the textbox. And lastly we create a division where we will show the result.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now, moving on to the JavaScript file. Name it “search.js”</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">function ajaxSearch() {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">var ajax;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">try {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">ajax = new XMLHttpRequest();</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">} catch (e) {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">try {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">ajax = new ActiveXObject (&#8220;Msxml2.XMLHTTP&#8221;);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">} catch (e) {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">try {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">ajax = new ActiveXObject (&#8220;Microsoft.XMLHTTP&#8221;);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">} catch (e) {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">alert (&#8220;ERROR: Your browser does not support AJAX.&#8221;);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">return false;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">ajax.onreadystatechange = function () {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">if (ajax.readyState == 4) { // when the action is complete.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">var ajaxSearchResults = document.getElementById(&#8220;ajaxDiv&#8221;); // the place holder of the result.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">ajaxSearchResults.innerHTML = ajax.responseText;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">var searchQuery = document.getElementById(&#8220;searchQuery&#8221;).value; // get the value from the textbox.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">var queryString = &#8220;?searchQuery=&#8221; + searchQuery; //add the string with the variable to pass to the file.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">ajax.open (&#8220;GET&#8221;, &#8220;Search.php&#8221; + queryString, true); // set the method to send the variable and the filename attached with the variable.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">ajax.send (null);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">I have written what a specific line of code does along the line itself. Well the basic concept is we take the value from the textbox and attach it to the string and pass it to the file. And we specify the path to show the result.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now creating the PHP file to find the result. Name it as “search.php”.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;?php</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$conn=mysql_connect(&#8216;localhost&#8217;,'root&#8217;,&#8221;) or die(&#8220;&lt;h5 align=center&gt;Could Not Connect to the Server&lt;/h5&gt;&#8221;);  // Connect to the Local server. Replace the values in the bracket with your server name, database username and password.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$db=mysql_select_db(&#8220;recess1&#8243;,$conn) or die(&#8220;&lt;h5 align=center&gt;Could Not Find the Databassse&lt;/h5&gt;&#8221;);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">// Connects to the db. replace “recess1” with your database name</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$searchQuery = $_GET['searchQuery']; // Get the value from the variable.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$resultDiv = &#8220;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">if ($searchQuery != &#8220;&#8221; || $searchQuery != null) { //check if value is null</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$result = mysql_query(&#8220;SELECT * FROM category WHERE name like &#8216;$searchQuery%&#8217; ORDER BY id desc&#8221;);  // Replace category with your tablename. And make modifications as your need.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$resultDiv = &#8220;&lt;div style =&#8217;padding-bottom: 10px;&#8217;&gt;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$resultDiv .= mysql_num_rows($result); // count the number of rows returned by the query</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$resultDiv .= &#8221; entries found for &#8220;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$resultDiv .= &#8216;&#8221;&#8216; . $searchQuery . &#8216;&#8221;&#8216;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$resultDiv .= &#8220;&lt;/div&gt;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">if ($result != false || $result || (mysql_num_rows ($result)&gt; 0)) { //if more than 0 results are found then display them in select menu.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$i = 1;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">?&gt;&lt;select&gt; //start the select box.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;?php</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>while ($row = mysql_fetch_array ($result)) {  // while there are rows</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>?&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;option&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;?php</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>echo $row['name'];  // display the data.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>?&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;/option&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;?php</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>$str = &#8220;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$i++;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>?&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;/select&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;?php</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo $resultDiv;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">?&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So what we do in this PHP file is that collect the value passed by the Get method and search for it in the database, and show it in the select menu.  So at the end of this you have a Ajax powered Page that updates its select menu with the change in textbox.  Find the source code of the working example here.</div>
<p>Today in this tutorial I shall teach you how to change the values of select menu with the change in textbox.  For example if we type “a” in the textbox than the select menu should show all the names starting from “a” without loading the page and continue as the change in textbox.</p>
<p>Ok so let us start. First make the input form and call it “form.php”</p>
<p>&lt;script language=&#8221;javascript&#8221; type=&#8221;text/javascript&#8221; src=&#8221;search.js&#8221;&gt;&lt;/script&gt;</p>
<p><span id="more-12"></span></p>
<p>&lt;form name=&#8217;myForm&#8217; style=&#8217;padding-top: 10px; padding-bottom: 7px;&#8217;&gt;</p>
<p>Type Value here:</p>
<p>&lt;input type=&#8217;text&#8217; name=&#8217;searchQuery&#8217; id=&#8217;searchQuery&#8217; onkeyup=&#8221;ajaxSearch();&#8221; /&gt;&amp;nbsp;</p>
<p>&lt;/form&gt;</p>
<p>&lt;div id=&#8217;ajaxDiv&#8217;&gt;&lt;?php include(&#8217;search.php&#8217;); ?&gt;&lt;/div&gt;</p>
<p>In the above form we first link the JavaScript file that will have the action for our job. The next part we create a form with textbox and assign an action on to it on “onkeyup” event. This will do the action when any new letter is typed in the textbox. And lastly we create a division where we will show the result.</p>
<p>Now, moving on to the JavaScript file. Name it “search.js”</p>
<p>function ajaxSearch() {</p>
<p>var ajax;</p>
<p>try {</p>
<p>ajax = new XMLHttpRequest();</p>
<p>} catch (e) {</p>
<p>try {</p>
<p>ajax = new ActiveXObject (&#8220;Msxml2.XMLHTTP&#8221;);</p>
<p>} catch (e) {</p>
<p>try {</p>
<p>ajax = new ActiveXObject (&#8220;Microsoft.XMLHTTP&#8221;);</p>
<p>} catch (e) {</p>
<p>alert (&#8220;ERROR: Your browser does not support AJAX.&#8221;);</p>
<p>return false;</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>ajax.onreadystatechange = function () {</p>
<p>if (ajax.readyState == 4) { // when the action is complete.</p>
<p>var ajaxSearchResults = document.getElementById(&#8220;ajaxDiv&#8221;); // the place holder of the result.</p>
<p>ajaxSearchResults.innerHTML = ajax.responseText;</p>
<p>}</p>
<p>}</p>
<p>var searchQuery = document.getElementById(&#8220;searchQuery&#8221;).value; // get the value from the textbox.</p>
<p>var queryString = &#8220;?searchQuery=&#8221; + searchQuery; //add the string with the variable to pass to the file.</p>
<p>ajax.open (&#8220;GET&#8221;, &#8220;Search.php&#8221; + queryString, true); // set the method to send the variable and the filename attached with the variable.</p>
<p>ajax.send (null);</p>
<p>}</p>
<p>I have written what a specific line of code does along the line itself. Well the basic concept is we take the value from the textbox and attach it to the string and pass it to the file. And we specify the path to show the result.</p>
<p>Now creating the PHP file to find the result. Name it as “search.php”.</p>
<p>&lt;?php</p>
<p>$conn=mysql_connect(&#8216;localhost&#8217;,'root&#8217;,&#8221;) or die(&#8220;&lt;h5 align=center&gt;Could Not Connect to the Server&lt;/h5&gt;&#8221;);  // Connect to the Local server. Replace the values in the bracket with your server name, database username and password.</p>
<p>$db=mysql_select_db(&#8220;recess1&#8243;,$conn) or die(&#8220;&lt;h5 align=center&gt;Could Not Find the Databassse&lt;/h5&gt;&#8221;);</p>
<p>// Connects to the db. replace “recess1” with your database name</p>
<p>$searchQuery = $_GET['searchQuery']; // Get the value from the variable.</p>
<p>$resultDiv = &#8220;&#8221;;</p>
<p>if ($searchQuery != &#8220;&#8221; || $searchQuery != null) { //check if value is null</p>
<p>$result = mysql_query(&#8220;SELECT * FROM category WHERE name like &#8216;$searchQuery%&#8217; ORDER BY id desc&#8221;);  // Replace category with your tablename. And make modifications as your need.</p>
<p>$resultDiv = &#8220;&lt;div style =&#8217;padding-bottom: 10px;&#8217;&gt;&#8221;;</p>
<p>$resultDiv .= mysql_num_rows($result); // count the number of rows returned by the query</p>
<p>$resultDiv .= &#8221; entries found for &#8220;;</p>
<p>$resultDiv .= &#8216;&#8221;&#8216; . $searchQuery . &#8216;&#8221;&#8216;;</p>
<p>$resultDiv .= &#8220;&lt;/div&gt;&#8221;;</p>
<p>if ($result != false || $result || (mysql_num_rows ($result)&gt; 0)) { //if more than 0 results are found then display them in select menu.</p>
<p>$i = 1;</p>
<p>?&gt;&lt;select&gt; //start the select box.</p>
<p>&lt;?php</p>
<p><span style="white-space: pre;"> </span>while ($row = mysql_fetch_array ($result)) {  // while there are rows</p>
<p><span style="white-space: pre;"> </span>?&gt;</p>
<p>&lt;option&gt;</p>
<p>&lt;?php</p>
<p><span style="white-space: pre;"> </span>echo $row['name'];  // display the data.</p>
<p><span style="white-space: pre;"> </span>?&gt;</p>
<p>&lt;/option&gt;</p>
<p>&lt;?php</p>
<p><span style="white-space: pre;"> </span>$str = &#8220;&#8221;;</p>
<p>$i++;</p>
<p>}</p>
<p><span style="white-space: pre;"> </span>?&gt;</p>
<p>&lt;/select&gt;</p>
<p>&lt;?php</p>
<p>}</p>
<p>echo $resultDiv;</p>
<p>}</p>
<p>?&gt;</p>
<p>So what we do in this PHP file is that collect the value passed by the Get method and search for it in the database, and show it in the select menu.  So at the end of this you have a Ajax powered Page that updates its select menu with the change in textbox.</p>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-love">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://rathour.com.np/blog/ajax-and-php-change-the-value-of-select-menu-with-the-change-in-textbox/&amp;title=Ajax+and+PHP+change+the+value+of+select+menu+with+the+change+in+textbox" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://rathour.com.np/blog/ajax-and-php-change-the-value-of-select-menu-with-the-change-in-textbox/&amp;title=Ajax+and+PHP+change+the+value+of+select+menu+with+the+change+in+textbox" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-reddit">
			<a href="http://reddit.com/submit?url=http://rathour.com.np/blog/ajax-and-php-change-the-value-of-select-menu-with-the-change-in-textbox/&amp;title=Ajax+and+PHP+change+the+value+of+select+menu+with+the+change+in+textbox" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://rathour.com.np/blog/ajax-and-php-change-the-value-of-select-menu-with-the-change-in-textbox/&amp;title=Ajax+and+PHP+change+the+value+of+select+menu+with+the+change+in+textbox" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://rathour.com.np/blog/ajax-and-php-change-the-value-of-select-menu-with-the-change-in-textbox/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://rathour.com.np/blog/ajax-and-php-change-the-value-of-select-menu-with-the-change-in-textbox/&amp;t=Ajax+and+PHP+change+the+value+of+select+menu+with+the+change+in+textbox" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Ajax+and+PHP+change+the+value+of+select+menu+with+the+change+in+textbox+-+http://b2l.me/fyjk7+" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

]]></content:encoded>
			<wfw:commentRss>http://rathour.com.np/blog/ajax-and-php-change-the-value-of-select-menu-with-the-change-in-textbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to create pagination? Paginate in PHP</title>
		<link>http://rathour.com.np/blog/how-to-create-pagination-paginate-in-php/</link>
		<comments>http://rathour.com.np/blog/how-to-create-pagination-paginate-in-php/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 13:14:02 +0000</pubDate>
		<dc:creator>Utsav</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://rathour.com.np/blog/?p=8</guid>
		<description><![CDATA[Okay so how many of you have been stuck in situations where you have plenty of records to display from a table and you cannot divide the records into pages. And your page becomes as long as “Great Wall of China”.  This has been situation for most of the people at least once. So today [...]]]></description>
			<content:encoded><![CDATA[<p>Okay so how many of you have been stuck in situations where you have plenty of records to display from a table and you cannot divide the records into pages. And your page becomes as long as “Great Wall of China”.  This has been situation for most of the people at least once. So today I am going to show you how to create pagination in PHP.</p>
<p><span id="more-8"></span></p>
<p>Well here is the code that you can use to create pagination. I have explained the code with the help of comments for each line, so won’t be explaining too much here.</p>
<p>But remember to replace the fields in the connection parameter with your values.</p>
<p>&lt;?php</p>
<p>// code for connecting to database. Replace with your connection parameters.</p>
<p>$conn=mysql_connect(&#8216;localhost&#8217;,'username&#8217;,'password&#8217;) or die(&#8220;Could Not Connect to server&#8221;);</p>
<p>$db=mysql_select_db(&#8220;database name&#8221;,$conn) or die(&#8220;Database not found.&#8221;);</p>
<p>$sql=&#8221;select * from tablename&#8221;;</p>
<p>$result1=mysql_query($sql);</p>
<p>$rows = mysql_num_rows($result1);</p>
<p>// Get the current Pagenumber</p>
<p>if(isset($_GET['pagenum']))</p>
<p>{</p>
<p><span style="white-space: pre;"> </span>$pagenum = $_GET['pagenum'];</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p><span style="white-space: pre;"> </span>$pagenum = 1; // if pagenumber is not set, then set it to 1.</p>
<p>}</p>
<p>//This is the number of results displayed per page</p>
<p>$page_rows = 4;</p>
<p>//This tells us the page number of our last page</p>
<p>$last = ceil($rows/$page_rows);</p>
<p>//this makes sure the page number isn&#8217;t below one, or more than our maximum pages</p>
<p>if ($pagenum &lt; 1)</p>
<p>{</p>
<p>$pagenum = 1;</p>
<p>}</p>
<p>elseif ($pagenum &gt; $last)</p>
<p>{</p>
<p>$pagenum = $last;</p>
<p>}</p>
<p>// set the limit for search as how many search from which record. and query again.</p>
<p>$offset = ($pagenum &#8211; 1) * $page_rows;</p>
<p>$data_p = mysql_query(&#8220;SELECT * FROM tablename&#8221;.&#8221;  LIMIT $offset, $page_rows&#8221;) or die(&#8220;No data found!!&#8221;);</p>
<p>// Show Records.</p>
<p>while($res=mysql_fetch_array($data_p))</p>
<p>{</p>
<p><span style="white-space: pre;"> </span>echo $res["columnname"].&#8221;&lt;/br&gt;&#8221;;</p>
<p>}</p>
<p>// showing pagination links.</p>
<p>echo &#8220;&lt;div&gt;&lt;ul&gt;&#8221;;</p>
<p>echo &#8220;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt; &#8211;You are currently at page $pagenum of $last&#8211; &lt;/font&gt;&lt;p&gt;&#8221;;</p>
<p>// First we check if we are on page one. If we are then we don&#8217;t need a link to the previous page or the first page so we do nothing. If we aren&#8217;t then we generate links to the first page, and to the previous page.</p>
<p>if ($pagenum == 1)</p>
<p>{</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>echo &#8220;&lt;li&gt; &lt;a href=&#8217;{$_SERVER['PHP_SELF']}? pagenum=1&#8242;&gt;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt;  First &lt;/font&gt;&lt;/a&gt; &lt;/li&gt;&#8221;;</p>
<p>echo &#8221; &#8220;;</p>
<p>$previous = $pagenum-1;</p>
<p>echo &#8220;&lt;li&gt; &lt;a href=&#8217;{$_SERVER['PHP_SELF']}?pagenum=$previous&#8217;&gt;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt;  Previous &lt;/font&gt;&lt;/a&gt; &lt;/li&gt;&#8221;;</p>
<p>echo &#8221; &#8220;;</p>
<p>}</p>
<p>//just a spacer</p>
<p>echo &#8221;  &#8221;;</p>
<p>//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links</p>
<p>if ($pagenum == $last)</p>
<p>{</p>
<p>}</p>
<p>else {</p>
<p>$next = $pagenum+1;</p>
<p>echo &#8220;&lt;li&gt; &lt;a href=&#8217;{$_SERVER['PHP_SELF']}? pagenum=$next&#8217;&gt;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt;  Next &lt;/font&gt;&lt;/a&gt;&lt;/li&gt; &#8220;;</p>
<p>echo &#8221; &#8220;;</p>
<p>echo &#8220;&lt;li&gt; &lt;a href=&#8217;{$_SERVER['PHP_SELF']}? pagenum=$last&#8217;&gt;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt;  Last &lt;/font&gt;&lt;/a&gt;&lt;/li&gt; &#8220;;</p>
<p>}</p>
<p>echo &#8220;&lt;/ul&gt;&lt;/div&gt;&#8221;;</p>
<p>?&gt;</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Pagination in PHP</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Okay so how many of you have been stuck in situations where you have plenty of records to display from a table and you cannot divide the records into pages. And your page becomes as long as “Great Wall of China”.  This has been situation for most of the people at least once. So today I am going to show you how to create pagination in PHP.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Well here is the code that you can use to create pagination. I have explained the code with the help of comments for each line, so won’t be explaining here.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">But remember to replace the fields in the connection parameter with your values.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;?php</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">// code for connecting to database. Replace with your connection parameters.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$conn=mysql_connect(&#8216;localhost&#8217;,'username&#8217;,'password&#8217;) or die(&#8220;Could Not Connect to server&#8221;);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$db=mysql_select_db(&#8220;database name&#8221;,$conn) or die(&#8220;Database not found.&#8221;);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$sql=&#8221;select * from tablename&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$result1=mysql_query($sql);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$rows = mysql_num_rows($result1);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">// Get the current Pagenumber</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">if(isset($_GET['pagenum']))</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>$pagenum = $_GET['pagenum'];</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">else</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>$pagenum = 1; // if pagenumber is not set, then set it to 1.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">//This is the number of results displayed per page</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$page_rows = 4;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">//This tells us the page number of our last page</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$last = ceil($rows/$page_rows);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">//this makes sure the page number isn&#8217;t below one, or more than our maximum pages</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">if ($pagenum &lt; 1)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$pagenum = 1;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">elseif ($pagenum &gt; $last)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$pagenum = $last;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">// set the limit for search as how many search from which record. and query again.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$offset = ($pagenum &#8211; 1) * $page_rows;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$data_p = mysql_query(&#8220;SELECT * FROM tablename&#8221;.&#8221;  LIMIT $offset, $page_rows&#8221;) or die(&#8220;No data found!!&#8221;);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">// Show Records.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">while($res=mysql_fetch_array($data_p))</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>echo $res["columnname"].&#8221;&lt;/br&gt;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">// showing pagination links.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8220;&lt;div&gt;&lt;ul&gt;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8220;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt; &#8211;You are currently at page $pagenum of $last&#8211; &lt;/font&gt;&lt;p&gt;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">// First we check if we are on page one. If we are then we don&#8217;t need a link to the previous page or the first page so we do nothing. If we aren&#8217;t then we generate links to the first page, and to the previous page.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">if ($pagenum == 1)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">else</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8220;&lt;li&gt; &lt;a href=&#8217;{$_SERVER['PHP_SELF']}? pagenum=1&#8242;&gt;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt;  First &lt;/font&gt;&lt;/a&gt; &lt;/li&gt;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8221; &#8220;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$previous = $pagenum-1;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8220;&lt;li&gt; &lt;a href=&#8217;{$_SERVER['PHP_SELF']}?pagenum=$previous&#8217;&gt;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt;  Previous &lt;/font&gt;&lt;/a&gt; &lt;/li&gt;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8221; &#8220;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">//just a spacer</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8221;  &#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">if ($pagenum == $last)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">else {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$next = $pagenum+1;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8220;&lt;li&gt; &lt;a href=&#8217;{$_SERVER['PHP_SELF']}? pagenum=$next&#8217;&gt;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt;  Next &lt;/font&gt;&lt;/a&gt;&lt;/li&gt; &#8220;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8221; &#8220;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8220;&lt;li&gt; &lt;a href=&#8217;{$_SERVER['PHP_SELF']}? pagenum=$last&#8217;&gt;&lt;font face=&#8217;verdana&#8217; size = &#8216;2&#8242; &gt;  Last &lt;/font&gt;&lt;/a&gt;&lt;/li&gt; &#8220;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">echo &#8220;&lt;/ul&gt;&lt;/div&gt;&#8221;;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">?&gt;</div>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-love">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://rathour.com.np/blog/how-to-create-pagination-paginate-in-php/&amp;title=How+to+create+pagination%3F+Paginate+in+PHP" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://rathour.com.np/blog/how-to-create-pagination-paginate-in-php/&amp;title=How+to+create+pagination%3F+Paginate+in+PHP" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-reddit">
			<a href="http://reddit.com/submit?url=http://rathour.com.np/blog/how-to-create-pagination-paginate-in-php/&amp;title=How+to+create+pagination%3F+Paginate+in+PHP" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://rathour.com.np/blog/how-to-create-pagination-paginate-in-php/&amp;title=How+to+create+pagination%3F+Paginate+in+PHP" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://rathour.com.np/blog/how-to-create-pagination-paginate-in-php/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://rathour.com.np/blog/how-to-create-pagination-paginate-in-php/&amp;t=How+to+create+pagination%3F+Paginate+in+PHP" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=How+to+create+pagination%3F+Paginate+in+PHP+-+http://b2l.me/fyks2+" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

]]></content:encoded>
			<wfw:commentRss>http://rathour.com.np/blog/how-to-create-pagination-paginate-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to encrypt password in C# using md5</title>
		<link>http://rathour.com.np/blog/how-to-encrypt-password-in-c-using-md5/</link>
		<comments>http://rathour.com.np/blog/how-to-encrypt-password-in-c-using-md5/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 13:01:47 +0000</pubDate>
		<dc:creator>Utsav</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://rathour.com.np/blog/?p=3</guid>
		<description><![CDATA[There must have been times when you needed to encrypt your data while using c# (…obviously that’s why you are reading this tutorial.) but didn’t know how to. Well this had been a question for me when I was doing my minor project for college and had to search a lot for it. And believe [...]]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">There must have been times when you needed to encrypt your data while using c# (…obviously that’s why you are reading this tutorial.) but didn’t know how to. Well this had been a question for me when I was doing my minor project for college and had to search a lot for it. And believe me it was very hard to find a good tutorial on it. So now I am going to provide you with the code and method to encrypt your data using md5 in C#.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">To start off, open a project in C# (a new one or your existing one). Now Right click on the project name and add a new class on to it, name it “function.cs”. As shown in the figure.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Fig1. Click on your poject name and click Add.               Fig2. Select Class from template and give name.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Part1. Creating the Function.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">To use md5 class you first need to call the Security namespace. So call it as:</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">MessageBox.Show(hash); //show the encrypted value</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">This shall give you output as follows.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So now you are done with the encryption technique. Now you can encrypt your password and other things and store it more securely in the database.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">I hope this article was helpful to you. I shall be back with more tutorials till then have a nice time.</div>
<p>There must have been times when you needed to encrypt your data while using c# (…obviously that’s why you are reading this tutorial.) but didn’t know how to. Well this had been a question for me when I was doing my minor project for college and had to search a lot for it. And believe me it was very hard to find a good tutorial on it. So now I am going to provide you with the code and method to encrypt your data using md5 in C#.</p>
<p><span id="more-3"></span></p>
<p><img class="size-medium wp-image-4 alignleft" style="margin: 6px;" title="encrypted" src="http://rathour.com.np/blog/wp-content/uploads/2010/02/encrypted-211x300.jpg" alt="encrypted" width="211" height="300" /></p>
<p>To start off, open a project in C# (a new one or your existing one). Now Right click on the project name and add a new class on to it, name it “function.cs”.</p>
<p>Part1. Creating the Function.</p>
<p>To use md5 class you first need to call the Security namespace. So call it as:</p>
<p><span style="font-family: monospace; line-height: 24px; font-size: 15px; color: #444444;">[sou<span>rcecode lang</span>uage="c#"]</span>using System.Security;</p>
<p>using System.Security.Cryptography;</p>
<p>Along with other previously called namespaces.</p>
<p>Now in the function class insert this code.</p>
<p>class functions</p>
<p>{</p>
<p>public static string getMd5Hash(string input)</p>
<p>{</p>
<p>// Create a new instance of the MD5CryptoServiceProvider object.</p>
<p>MD5 md5Hasher = MD5.Create();</p>
<p>// Convert the input string to a byte array and compute the hash.</p>
<p>byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));</p>
<p>// Create a new Stringbuilder to collect the bytes</p>
<p>// and create a string.</p>
<p>StringBuilder sBuilder = new StringBuilder();</p>
<p>// Loop through each byte of the hashed data</p>
<p>// and format each one as a hexadecimal string.</p>
<p>for (int i = 0; i &lt; data.Length; i++)</p>
<p>{</p>
<p>sBuilder.Append(data[i].ToString(&#8220;x2&#8243;));</p>
<p>}</p>
<p>// Return the hexadecimal string.</p>
<p>return sBuilder.ToString();</p>
<p>}</p>
<p>}</p>
<p><span style="font-family: monospace; line-height: 24px; font-size: 15px; color: #444444;">[/sourc<span>ecode]</span></span></p>
<p>So now you have created the function to encrypt the given data. Now you can call this from anywhere in your project to encrypt the data.</p>
<p>Part2. Calling the Function.</p>
<p>Now, for encrypting the data. Add a new form in the project. Add two text fields, two labels and a button on to the form.</p>
<p>Double click on the button to go to its Code window. Now call the function you created as.</p>
<p>string source = textBox1.Text; // get the value of textbox1</p>
<p>string hash = functions.getMd5Hash(source); // Pass the data to the function and store the encrypted value at new variable</p>
<p>MessageBox.Show(hash); //show the encrypted value</p>
<p>This shall give you output as follows.</p>
<p>So now you are done with the encryption technique. Now you can encrypt your password and other things and store it more securely in the database.</p>
<p>I hope this article was helpful to you. I shall be back with more tutorials till then have a nice time.</p>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-love">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://rathour.com.np/blog/how-to-encrypt-password-in-c-using-md5/&amp;title=How+to+encrypt+password+in+C%23+using+md5+" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://rathour.com.np/blog/how-to-encrypt-password-in-c-using-md5/&amp;title=How+to+encrypt+password+in+C%23+using+md5+" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-reddit">
			<a href="http://reddit.com/submit?url=http://rathour.com.np/blog/how-to-encrypt-password-in-c-using-md5/&amp;title=How+to+encrypt+password+in+C%23+using+md5+" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://rathour.com.np/blog/how-to-encrypt-password-in-c-using-md5/&amp;title=How+to+encrypt+password+in+C%23+using+md5+" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://rathour.com.np/blog/how-to-encrypt-password-in-c-using-md5/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://rathour.com.np/blog/how-to-encrypt-password-in-c-using-md5/&amp;t=How+to+encrypt+password+in+C%23+using+md5+" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=How+to+encrypt+password+in+C%23+using+md5++-+http://b2l.me/fyjy6+" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

]]></content:encoded>
			<wfw:commentRss>http://rathour.com.np/blog/how-to-encrypt-password-in-c-using-md5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
