<?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>vijayjoshi.org &#187; FAQ</title>
	<atom:link href="http://www.vijayjoshi.org/category/faq/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vijayjoshi.org</link>
	<description>php &#124; javascript &#124; ajax &#124; and all things web</description>
	<lastBuildDate>Sun, 20 Nov 2011 15:24:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Comment box not visible on a page/post in WordPress 3.2.1</title>
		<link>http://www.vijayjoshi.org/2011/10/21/comment-box-not-visible-on-a-pagepost-in-wordpress-3-2-1/</link>
		<comments>http://www.vijayjoshi.org/2011/10/21/comment-box-not-visible-on-a-pagepost-in-wordpress-3-2-1/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 11:09:22 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=1312</guid>
		<description><![CDATA[
			
				
			
		
First of all I will congratulate you on upgrading to wordpress 3.2.1. Having latest version means your site is updated and safe.
Now coming to the point, some of you might have faced this problem. You create a new page or post and do not want to allow comments on that. So you scroll down but cannot find the discussion box. ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F10%2F21%2Fcomment-box-not-visible-on-a-pagepost-in-wordpress-3-2-1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F10%2F21%2Fcomment-box-not-visible-on-a-pagepost-in-wordpress-3-2-1%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>First of all I will congratulate you on upgrading to wordpress 3.2.1. Having latest version means your site is updated and safe.</p>
<p>Now coming to the point, some of you might have faced this problem. You create a new page or post and do not want to allow comments on that. So you scroll down but cannot find the discussion box. The box which has 2 checkboxes &#8220;Allow comments&#8221; and &#8220;Allow trackbacks and pingbacks&#8221;.</p>
<p>To fix this, look at the top right of your screen for the <strong>Screen Options</strong> button. Click that and you will see a lot of checkboxes there. Click the <strong>Discussion</strong>  checkbox and close the screen options. Now scroll to bottom of the page and you will find the box sitting there.</p>
<p><a href="http://www.vijayjoshi.org/wp-content/uploads/wp-comments.png"><img class="aligncenter size-full wp-image-1313" title="wp-comments" src="http://www.vijayjoshi.org/wp-content/uploads/wp-comments.png" alt="Enable comments" width="656" height="268" /></a></p>
<p>WP3.2.1 has several other options like Custom fields, slug, Author which you can also enable from here.</p>
<div class="shr-publisher-1312"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2011%2F10%2F21%2Fcomment-box-not-visible-on-a-pagepost-in-wordpress-3-2-1%2F' data-shr_title='Comment+box+not+visible+on+a+page%2Fpost+in+Wordpress+3.2.1'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2011/10/21/comment-box-not-visible-on-a-pagepost-in-wordpress-3-2-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to create excel files in PHP?</title>
		<link>http://www.vijayjoshi.org/2009/12/15/how-to-create-excel-files-in-php/</link>
		<comments>http://www.vijayjoshi.org/2009/12/15/how-to-create-excel-files-in-php/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 09:04:47 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=454</guid>
		<description><![CDATA[
			
				
			
		
Some time ago I found a class on phpclasses.org by Harish Chauhan which makes it incredibly easy to generate simple excel spreadsheets from PHP. I found it useful so I want to share its usgae with readers too. I also made some changes to the class for styling the rows.



Here is the process:
1-  Include the ExcelWriter class file in ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F12%2F15%2Fhow-to-create-excel-files-in-php%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F12%2F15%2Fhow-to-create-excel-files-in-php%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Some time ago I found a class on phpclasses.org by Harish Chauhan which makes it incredibly easy to generate simple excel spreadsheets from PHP. I found it useful so I want to share its usgae with readers too. I also made some changes to the class for styling the rows.</p>
<p><a href="http://www.vijayjoshi.org/examples/excelwriter.zip" title="Download Source Code"><br />
<img style="border: 0px none ;" src="http://www.vijayjoshi.org/wp-content/uploads/download.png" alt="Download Source" /><br />
</a></p>
<p>Here is the process:</p>
<h4>1-  Include the ExcelWriter class file in your code</h4>
<pre class="brush:php">include("excelwriter.inc.php");</pre>
<h4>2- Initialize the ExcelWriter class</h4>
<pre class="brush:php">$fileName = "myNewExcel.xls";
	$excel = new ExcelWriter($fileName);</pre>
<p>Constructor of ExcelWriter class accepts a file name as parameter. File will be saved by this name.</p>
<p><strong><span id="more-454"></span></strong></p>
<h4>3- Write data to the file</h4>
<p>There are 2 ways to write data to the file. One row at a time or column by column.<br />
<br/><br />
<strong>-Writing one complete row: </strong></p>
<pre class="brush:php">$myArr=array("<strong>First Name</strong>",
				"Last Name",
				"Address",
				"Age"
				);

	$excel->writeLine($myArr, array('text-align'=>'center', 'color'=> 'red'));</pre>
<p>First create an array of all values and use the writeLine method to write it to file. WriteLine accepts 2 parameters. First is an array of values that you wish to write in a row. Second parameter, which is optional, is the array of style properties that you may wish to apply to whole row. All css properties will work as long as you pass them in array format.</p>
<p><strong> -Writing column by column data:</strong><br />
You can write data on column basis too. To do this create a row first and then write columns as required.</p>
<pre class="brush:php">
// this will create an empty row
	$excel->writeRow();
	// Now write columns
	$excel->writeCol("Alok");
	// inline styling can be done too
	$excel->writeCol("<span style="color:red;">Sah</span>");
	// or you can pass array of css properties
	$excel->writeCol("Pandav Nagar", array('text-align'=>'center', 'color'=> 'green','font-size'=> '22px'));
	$excel->writeCol(30,array('text-align'=>'right', 'color'=> 'brown'));</pre>
<p>WriteCol writes data to a single cell. Just like WriteLine, WriteCol also accepts 2 parameters. First is column value and second(optional) is css properties.</p>
<h4>4- Close the file and you are done</h4>
<pre class="brush:php">$excel->close();</pre>
<p>After you are done with writing all data, don&#8217;t forget to close the file. Your excel file is ready now.</p>
<p>One more thing. You can style the column data in 2 ways. Either pass an array of css properties to WriteLine() method or wrap the column data in a html element.</p>
<p>Also make sure that you have write permission set for your excel file.<br />
Complete source code for example:</p>
<pre class="brush:php">include("excelwriter.inc.php");
	$fileName = "mynewxls.xls";
	$excel = new ExcelWriter($fileName);

	if($excel==false)
	{
		echo $excel->error;
		die;
	}
	$myArr=array(
				"<strong>First Name</strong>",
				"Last Name",
				"Address",
				"Age"
				);

	$excel->writeLine($myArr, array('text-align'=>'center', 'color'=> 'red'));

	$myArr=array("Vijay","Joshi","<span style="color:blue;">Vikaspuri</span>, Delhi",27);
	$excel->writeLine($myArr);

	$excel->writeRow();
	$excel->writeCol("Alok");
	$excel->writeCol("<span style="color:red;">Sah</span>");
	$excel->writeCol("Pandav Nagar", array('text-align'=>'center', 'color'=> 'green','font-size'=> '22px'));
	$excel->writeCol(30,array('text-align'=>'right', 'color'=> 'brown'));

	$excel->close();
	echo "Data written to file $fileName Successfully.";</pre>
<p><a href="http://www.vijayjoshi.org/examples/excelwriter.zip"   title="Download Source Code"><br />
<img style="border: 0px none ;" src="http://www.vijayjoshi.org/wp-content/uploads/download.png" alt="Download Source" /><br />
</a></p>
<p><em>Give this article a Thumbs-Up on StumbleUpon if you like this.</em></p>
<div class="shr-publisher-454"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F12%2F15%2Fhow-to-create-excel-files-in-php%2F' data-shr_title='How+to+create+excel+files+in+PHP%3F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/12/15/how-to-create-excel-files-in-php/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Getting and setting value of html elements using jQuery</title>
		<link>http://www.vijayjoshi.org/2009/12/09/getting-and-setting-value-of-html-elements-using-jquery/</link>
		<comments>http://www.vijayjoshi.org/2009/12/09/getting-and-setting-value-of-html-elements-using-jquery/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 05:48:16 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=446</guid>
		<description><![CDATA[
			
				
			
		
If you have started using jQuery recently and you  are used to DOM methods of getting and setting values for form and html elements, this article is for you.
Using jQuery can be confusing in the beginning but it is a lot easier once you get hold of it. In this post I will summarize methods for getting and setting values ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F12%2F09%2Fgetting-and-setting-value-of-html-elements-using-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F12%2F09%2Fgetting-and-setting-value-of-html-elements-using-jquery%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>If you have started using jQuery recently and you  are used to DOM methods of getting and setting values for form and html elements, this article is for you.</p>
<p>Using jQuery can be confusing in the beginning but it is a lot easier once you get hold of it. In this post I will summarize methods for getting and setting values for html elements using jQuery. I assume that you are familiar with the $ function. Though it can be used in different ways to select elements, we will stick to selecting elements by id for the purpose of this tutorial.</p>
<p><a href="http://www.vijayjoshi.org/examples/jQuery.html"><img src="http://www.vijayjoshi.org/wp-content/uploads/demo.png" alt="View Demo" style="border:0px"/></a></p>
<p>An element can be accessed using its id as follows:</p>
<pre class="brush:js">$('#someId')</pre>
<h3>Textbox, Hidden field and Textarea</h3>
<p>All these elements share a common method. val() method is used to get and set values for these elements.</p>
<p>Setting:</p>
<pre class="brush:js">$('#someid').val('any value')</pre>
<p>will set the value of textbox to &#8220;any value&#8221;.</p>
<p>Getting:</p>
<pre class="brush:js">$('#someid').val()</pre>
<p>will get the value of that text box.</p>
<h3>Radio Buttons and checkboxes</h3>
<p>attr() is used to check/uncheck and select/unselect radios and checkboxes</p>
<p>Setting:</p>
<pre class="brush:js">$('#id').attr('checked', true)</pre>
<p>will select the control</p>
<pre class="brush:js">$('#id').attr('checked', false)</pre>
<p>will unselect the control</p>
<p>Getting</p>
<pre class="brush:js">$('#id').attr('checked')</pre>
<p>This will give true if radio/checkbox is checked/selected, false otherwise.</p>
<h3>Select box/ Combo box</h3>
<p>A notorious control, jQuery makes it very easy to get and set value of a combo box.</p>
<p>Setting:</p>
<pre class="brush:js">$('#selectbox').val('1')</pre>
<p>Pass the value of option that you wish to select to val()</p>
<p>Getting:</p>
<pre class="brush:js">$('#selectbox').val()</pre>
<p>This will give value of currently selected option in combo box</p>
<h3>Setting/Getting innerHTML  of other elements</h3>
<p>Just like innerHTML, jQuery provides a handy function to manipulate innerHTML for all html elements.</p>
<p>Setting:</p>
<pre class="brush:js">$('#somediv').html('some html inside a paragraph')</pre>
<p>This will set the innerHTML of element with id somediv</p>
<p>Getting:</p>
<pre class="brush:js">$('#somediv').html()</pre>
<p>This will return the innerHTML of an element.</p>
<p>Thats all to it. Again, a demo can be found at <a title="View Demo" href="http://www.vijayjoshi.org/examples/jQuery.html">this link</a> (and obviously source code too). You can always shoot me a mail in case your question is not answered here.</p>
<p><em>Give this article a thumbs up on stumble upon if you liked this</em></p>
<div class="shr-publisher-446"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F12%2F09%2Fgetting-and-setting-value-of-html-elements-using-jquery%2F' data-shr_title='Getting+and+setting+value+of+html+elements+using+jQuery'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/12/09/getting-and-setting-value-of-html-elements-using-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Quick PHP Tip: How to parse CDATA sections using SimpleXML</title>
		<link>http://www.vijayjoshi.org/2009/09/22/quick-php-tip-how-to-parse-cdata-sections-using-simplexml/</link>
		<comments>http://www.vijayjoshi.org/2009/09/22/quick-php-tip-how-to-parse-cdata-sections-using-simplexml/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 08:41:39 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=393</guid>
		<description><![CDATA[
			
				
			
		
Applies to:  simplexml_load_string and simplexml_load_file
Problem : SimpleXML does not parse text inside CDATA tags in an XML.
Consider the XML below:


$str = '';
$str.='some text goes here';
$str.='';

To parse it we use following syntax:
$xml = simplexml_load_string($str);
On printing it outputs:


SimpleXMLElement Object
(
    [childNode] => some text goes here
)

Thats OK. Now the same xml but this time the text is enclosed in CDATA ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F09%2F22%2Fquick-php-tip-how-to-parse-cdata-sections-using-simplexml%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F09%2F22%2Fquick-php-tip-how-to-parse-cdata-sections-using-simplexml%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h4>Applies to:  simplexml_load_string and simplexml_load_file</h4>
<h4>Problem : SimpleXML does not parse text inside CDATA tags in an XML.</h4>
<p>Consider the XML below:</p>
<pre class="brush:php">

$str = '<rootNode>';
$str.='<childNode>some text goes here</childNode>';
$str.='</rootNode>';
</pre>
<p>To parse it we use following syntax:</p>
<pre class="brush:php">$xml = simplexml_load_string($str);</pre>
<p>On printing it outputs:</p>
<pre class="brush:php">

SimpleXMLElement Object
(
    [childNode] => some text goes here
)
</pre>
<p>Thats OK. Now the same xml but this time the text is enclosed in CDATA tags.</p>
<pre class="brush:php">

$str = '<rootNode>';
$str.='<childNode><![CDATA[some text goes here]]&gt;</childNode>';
$str.='</rootNode>'
</pre>
<p>On printing this gives following output:</p>
<pre class="brush:php">

SimpleXMLElement Object
(
    [childNode] => SimpleXMLElement Object
        (
        )

)
</pre>
<p>Yes its empty. This is because SimpleXML does not parse CDATA tags. All data enclosed within CDATA is ignored by SimpleXML parser.</p>
<h4>Solution: Set the 3rd parameter to LIBXML_NOCDATA while parsing.</h4>
<p>simplexml_load_string(simplexml_load_file too) actually takes 3 parameters.</p>
<ul>
<li>The string to parse</li>
<li>Optional parameter &#8211; to return an object of class specified in this parameter. (By default it returns a SimpleXMLElement Object)</li>
<li>Also optional &#8211; libxml parameters can be specified as options. This option provides the solution to our CDATA problem</li>
</ul>
<p>Provide the 3rd parameter LIBXML_NOCDATA and SimpleXML will consider CDATA nodes as text nodes and will parse them.</p>
<pre class="brush:php">

$xml = simplexml_load_string($str,'SimpleXMLElement', LIBXML_NOCDATA);
</pre>
<p>This will output as desired:</p>
<pre class="brush:php">

SimpleXMLElement Object
(
    [childNode] => some text goes here
)
</pre>
<p>Please note that using the third parameter requires PHP >=5.1 compiled with libxml.</p>
<p>I will be publishing a new post shortly on how to use SimpleXML to parse xml and extract data.</p>
<div class="shr-publisher-393"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F09%2F22%2Fquick-php-tip-how-to-parse-cdata-sections-using-simplexml%2F' data-shr_title='Quick+PHP+Tip%3A+How+to+parse+CDATA+sections+using+SimpleXML'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/09/22/quick-php-tip-how-to-parse-cdata-sections-using-simplexml/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FAQ(Javascript) : User confirmation on close button of browser</title>
		<link>http://www.vijayjoshi.org/2009/07/07/faqjavascript-user-confirmation-on-close-button-of-browser/</link>
		<comments>http://www.vijayjoshi.org/2009/07/07/faqjavascript-user-confirmation-on-close-button-of-browser/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 06:16:36 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=354</guid>
		<description><![CDATA[
			
				
			
		
Problem: On clicking the close button of browser ask for user confirmation with a custom message
Solution: Use the window.onbeforeunload method. This method expects a string value. This string will be displayed to user along with the default string. Default string that is displayed to user is:
Are you sure you want to navigate away from this page?
Press OK to continue, or ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F07%2F07%2Ffaqjavascript-user-confirmation-on-close-button-of-browser%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F07%2F07%2Ffaqjavascript-user-confirmation-on-close-button-of-browser%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Problem: On clicking the close button of browser ask for user confirmation with a custom message</strong></p>
<p><strong>Solution:</strong> Use the window.onbeforeunload method. This method expects a string value. This string will be displayed to user along with the default string. Default string that is displayed to user is:</p>
<blockquote><p>Are you sure you want to navigate away from this page?<br />
Press OK to continue, or Cancel to stay on the current page.
</p></blockquote>
<p>You can see an example in action <a href="http://www.vijayjoshi.org/examples/onbeforeunload.html">here</a>. Full code is given below.</p>
<pre class="brush:xml">
&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
  window.onbeforeunload = function()
   {
     return 'Please do not leave this page.';
   }

/*
alternatively, you can also write the following
this will also work the same as above
window.onbeforeunload = confirmFromUser;
function confirmFromUser()
{
	return 'Please do not leave this page.';
}
*/

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
	Try reloading this page or closing your browser. &lt;br/&gt;
        Also try back, forward buttons.
&lt;/p&gt;
&lt;p&gt;
	&lt;a href="http://google.com"&gt;Click me&lt;/a&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>onbeforeunload event will fire on reloading the page, using browser&#8217;s back/forward button or even on clicking an anchor that navigates user from current page to any other location.</p>
<div class="shr-publisher-354"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F07%2F07%2Ffaqjavascript-user-confirmation-on-close-button-of-browser%2F' data-shr_title='FAQ%28Javascript%29+%3A+User+confirmation+on+close+button+of+browser'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/07/07/faqjavascript-user-confirmation-on-close-button-of-browser/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Turn register globals on off using htaccess</title>
		<link>http://www.vijayjoshi.org/2009/06/05/php-turn-register-globals-on-off-using-htaccess/</link>
		<comments>http://www.vijayjoshi.org/2009/06/05/php-turn-register-globals-on-off-using-htaccess/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 10:59:37 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[htaccess]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=347</guid>
		<description><![CDATA[
			
				
			
		
Problem:
My application is hosted on a server where php has register_globals set to on in php.ini. But using register_globals = on makes code error prone and is dangerous to use.
I want to turn it of for my application regardless of what is its setting for other applications.
Since the value of register_globals cannot be set at run time bu using ini_set, ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F06%2F05%2Fphp-turn-register-globals-on-off-using-htaccess%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F06%2F05%2Fphp-turn-register-globals-on-off-using-htaccess%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Problem:</strong></p>
<p>My application is hosted on a server where php has register_globals set to on in php.ini. But using register_globals = on <a title="Register Globals Secutiry" href="http://in.php.net/manual/en/security.globals.php" target="_blank">makes code error prone and is dangerous to use</a>.<br />
I want to turn it of for my application regardless of what is its setting for other applications.<br />
Since the value of register_globals cannot be set at run time bu using ini_set, how can I do it?</p>
<p><strong>Solution:</strong></p>
<p>Use htaccess file to control this behaviour. Create a file named .htaccess in root directory of your application and write the following line in it:</p>
<pre class="brush:php">php_flag register_globals off</pre>
<p>Thats all you need to do.</p>
<p>You can verify this by creating a phpinfo() file. Suppose your server has register_globals set to on and you have turned it off using above syntax.</p>
<p>phpinfo() will show the local value as off and master value as on.</p>
<p>Note: Of course, this will work only if your apache configuration allows to override htaccess settings.</p>
<div class="shr-publisher-347"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F06%2F05%2Fphp-turn-register-globals-on-off-using-htaccess%2F' data-shr_title='PHP+-+Turn+register+globals+on+off+using+htaccess'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/06/05/php-turn-register-globals-on-off-using-htaccess/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FAQ: How to fix error 403: access forbidden for phpmyadmin in wampserver?</title>
		<link>http://www.vijayjoshi.org/2009/05/26/faq-how-to-fix-error-403-access-forbidden-for-phpmyadmin-in-wampserver/</link>
		<comments>http://www.vijayjoshi.org/2009/05/26/faq-how-to-fix-error-403-access-forbidden-for-phpmyadmin-in-wampserver/#comments</comments>
		<pubDate>Tue, 26 May 2009 06:31:25 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=336</guid>
		<description><![CDATA[
			
				
			
		
The Problem:
PhpMyadmin works fine on localhost but accessing it over a network gives error 403: Access Forbidden.
Reason:
Reason is pretty clear &#8211; phpmyadmin is not configured to be accessed over a network. Permission to access it over network is not given.
Solution:
Locate the file phpmyadmin.conf in your wamp installation. If you have wampserver version 2 or higher, this file can be found ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F05%2F26%2Ffaq-how-to-fix-error-403-access-forbidden-for-phpmyadmin-in-wampserver%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F05%2F26%2Ffaq-how-to-fix-error-403-access-forbidden-for-phpmyadmin-in-wampserver%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><em>The Problem:</em></p>
<p><strong>PhpMyadmin works fine on localhost but accessing it over a network gives error 403: Access Forbidden.</strong></p>
<p><em>Reason:</em></p>
<p><strong>Reason is pretty clear &#8211; phpmyadmin is not configured to be accessed over a network. Permission to access it over network is not given.</strong></p>
<p><em>Solution:</em></p>
<p>Locate the file phpmyadmin.conf in your wamp installation. If you have wampserver version 2 or higher, this file can be found at <strong>C:\wamp\alias\phpmyadmin.conf </strong>(Of course assuming that you installed wamp in C:\wamp). For earlier wamp versions this file is at <strong>C:\wamp\Apache2\conf\alias\phpmyadmin.conf</strong>.<br />
Once found,open this file. Contents of the file will be like the following:</p>
<pre class="brush:php">
Alias /phpmyadmin "c:/wamp/apps/phpmyadmin3.1.3.1/"
# to give access to phpmyadmin from outside
# replace the lines
#
#        Order Deny,Allow
#	Deny from all
#	Allow from 127.0.0.1
#
# by
#
#        Order Allow,Deny
#   Allow from all
#
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order Deny,Allow
	Deny from all
	Allow from 127.0.0.1</pre>
<p>As it is clearly written in the file itself, replace the line <strong>Deny from all</strong> (second line from the bottom )with <strong>Allow from all</strong>. Save the file, restart apache and you are done.</p>
<p>PhpMyadmin can be accessed from outside now.</p>
<div class="shr-publisher-336"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F05%2F26%2Ffaq-how-to-fix-error-403-access-forbidden-for-phpmyadmin-in-wampserver%2F' data-shr_title='FAQ%3A+How+to+fix+error+403%3A+access+forbidden+for+phpmyadmin+in+wampserver%3F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/05/26/faq-how-to-fix-error-403-access-forbidden-for-phpmyadmin-in-wampserver/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>FAQ : How to toggle visibility of an html element by javascript</title>
		<link>http://www.vijayjoshi.org/2009/01/27/faq-how-to-toggle-visibility-of-an-html-element-by-javascript/</link>
		<comments>http://www.vijayjoshi.org/2009/01/27/faq-how-to-toggle-visibility-of-an-html-element-by-javascript/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 09:37:48 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=264</guid>
		<description><![CDATA[
			
				
			
		
In my previous post, I discussed how we can change the style and/or css class of any html element by JavaScript. We can take advantage of this feature to add functionality where visibility of elements can be toggled. That means clicking a button will change the state of that element from visible to invisible and vice-versa.
The css property that we ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F01%2F27%2Ffaq-how-to-toggle-visibility-of-an-html-element-by-javascript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F01%2F27%2Ffaq-how-to-toggle-visibility-of-an-html-element-by-javascript%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In my <a title="Change css of elements from javascript" href="http://www.vijayjoshi.org/2009/01/24/setting-stylecss-class-of-html-elements-from-javascript/" target="_self">previous post</a>, I discussed how we can change the style and/or css class of any html element by JavaScript. We can take advantage of this feature to add functionality where visibility of elements can be toggled. That means clicking a button will change the state of that element from visible to invisible and vice-versa.</p>
<p>The css property that we will manipulate to achieve this behavior is &#8220;display&#8221;. Below is the html we will use for the example.</p>
<pre class="brush:js">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Toggle elements&lt;/title&gt;
&lt;script type="text/javascript" language="javascript"&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div style="border:2px solid;"&gt;
&lt;p&gt;&lt;input type="button" onClick="toggleMe('toggleHolmes');" value="Click to toggle"/&gt;&lt;/p&gt;
&lt;p id="toggleHolmes" style="display:block;"&gt;
I consider that a mans brain originally is like a little empty attic, and you have to stock it with such furniture as you choose. A fool takes in all the lumber of every sort that he comes across, so that the knowledge which might be useful to him gets crowded out, or at best is jumbled up with a lot of other things, so that he has a difficulty in laying his hands upon it.&lt;br/&gt;
-Sherlock Holmes (A Study in Scarlet - Part 1,chap. 2)
&lt;/p&gt;
&lt;/div&gt;
&lt;br/&gt;
&lt;div style="border:2px solid;"&gt;
&lt;p&gt;&lt;input type="button" onClick="toggleMe('toggleNietzsche');" value="Click to toggle"/&gt;&lt;/p&gt;
&lt;p id="toggleNietzsche" style="display:block;"&gt;
God is dead. God remains dead. And we have killed him. How shall we comfort ourselves, the murderers of all murderers? What was holiest and mightiest of all that the world has yet owned has bled to death under our knives: who will wipe this blood off us? What water is there for us to clean ourselves? What festivals of atonement, what sacred games shall we have to invent? Is not the greatness of this deed too great for us? Must we ourselves not become gods simply to appear worthy of it?&lt;br/&gt;
-"The Madman" (Thus Spoke Zarathustra by Friedrich Nietzsche)
&lt;/p&gt;

&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>There are 2 paragraphs with ids &#8220;toggleHolmes&#8221; and &#8220;toggleNietzsche&#8221; which contain some text. Above each of them is a button. Onclick of each of those buttons we have called a function toggleMe which accepts the id of the element which is to be toggled as its parameter.<br />
Now include the following JavaScript code between the script tags in above html.<br />
<span id="more-264"></span></p>
<pre class="brush:js">
function toggleMe(id)
{
	var element = document.getElementById(id);
	if(element.style.display == "none")
	{
		element.style.display = "block";
	}
	else
	{
		element.style.display = "none";
	}
}
</pre>
<p>Let us analyze it. Function toggleMe gets the id of element.Here we follow the following steps:<br />
Check whether the display property of element is already set to none.<br />
If it is none we need to change it to block.<br />
Otherwise it is visible and therefore we set it to none.</p>
<p>Now go ahead and click the button. Clicking button will make the respective paragraph visible and hidden alternatively.<br />
In case you are interested these all lines of toggleMe function can be replaced by a single line. The magic of <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Conditional_Operator" target="_blank">Conditional Operator</a> <img src='http://www.vijayjoshi.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre class="brush:js">
element.style.display = (element.style.display == "none") ? "block" : "none";
</pre>
<p>Above example can be seen in action at <a href="http://vijayjoshi.org/examples/toggle.html" target="_blank">http://vijayjoshi.org/examples/toggle.html</a></p>
<div class="shr-publisher-264"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F01%2F27%2Ffaq-how-to-toggle-visibility-of-an-html-element-by-javascript%2F' data-shr_title='FAQ+%3A+How+to+toggle+visibility+of+an+html+element+by+javascript'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/01/27/faq-how-to-toggle-visibility-of-an-html-element-by-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FAQ : Problem with javascript function parseInt</title>
		<link>http://www.vijayjoshi.org/2009/01/07/faq-problem-with-javascript-function-parseint/</link>
		<comments>http://www.vijayjoshi.org/2009/01/07/faq-problem-with-javascript-function-parseint/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 06:36:06 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=245</guid>
		<description><![CDATA[
			
				
			
		
JavaScript function parseInt() is used to parse a string and extract the integer from that string.
Therefore if you do parseInt(&#8220;200xyz&#8221;), it will return 200.
The problem arises when you do the following:
var value = "010";
alert(parseInt(value)); // incorrect: will alert 8
You had expected 10 but what you got is 8.
Weird behavior, you must be thinking.
Actually not.It is a lesser known fact that ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F01%2F07%2Ffaq-problem-with-javascript-function-parseint%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F01%2F07%2Ffaq-problem-with-javascript-function-parseint%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>JavaScript function parseInt() is used to parse a string and extract the integer from that string.<br />
Therefore if you do parseInt(&#8220;200xyz&#8221;), it will return 200.<br />
The problem arises when you do the following:</p>
<pre class="brush:js">var value = "010";
alert(parseInt(value)); // incorrect: will alert 8</pre>
<p>You had expected 10 but what you got is 8.</p>
<p>Weird behavior, you must be thinking.</p>
<p>Actually not.It is a lesser known fact that parseInt actually takes two arguments, second of which is optional.</p>
<p>First argument is the string to parse and second is radix, which can be any one of base 10, base 2, base 8 or others.</p>
<p>If second argument is not given. parseInt does not always assume it to be 10. It parses the string on following assumptions:</p>
<blockquote>
<ul>
<li>If the string starts with &#8220;0&#8243;, it assumes the radix to be 8 (octal numbers).</li>
<li>If the string starts with &#8220;0x&#8221;, it assumes the radix to be 16 (hexadecimal values).</li>
<li>In other cases it assumes radix as 10.</li>
</ul>
</blockquote>
<p>In the above case 010 means 8 in the octal number system.<br />
Value in Octal       Value in Decimal<br />
0                           0<br />
1                           1<br />
2                           2<br />
3                           3<br />
4                           4<br />
5                           5<br />
6                           6<br />
7                           7<br />
10                         8<br />
11                         9<br />
12                         10<br />
13                         11<br />
14                         12<br />
15                         13</p>
<p>So, to get integer 10 from string &#8220;010&#8243; always specify the base second argument.</p>
<pre class="brush:js">var value = "010";
alert(parseInt(value,10)); // correct: will alert 10</pre>
<p>So the rule of thumb is :</p>
<h3>Always specify the second parameter (radix) to parseInt function in order to get correct value.</h3>
<div class="shr-publisher-245"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F01%2F07%2Ffaq-problem-with-javascript-function-parseint%2F' data-shr_title='FAQ+%3A+Problem+with+javascript+function+parseInt'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/01/07/faq-problem-with-javascript-function-parseint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FAQ: How to upload files in php?</title>
		<link>http://www.vijayjoshi.org/2008/12/26/faq-how-to-upload-files-in-php/</link>
		<comments>http://www.vijayjoshi.org/2008/12/26/faq-how-to-upload-files-in-php/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 09:18:04 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=229</guid>
		<description><![CDATA[
			
				
			
		
Uploading files in web applications is a common practice. Thanks to php which makes it simple to implement.
This post will present a simple example of how to upload files using php.
Before any code, first let us understand the file upload process .
User is displayed a page with a file select box. She then selects a file and clicks on submit ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2008%2F12%2F26%2Ffaq-how-to-upload-files-in-php%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2008%2F12%2F26%2Ffaq-how-to-upload-files-in-php%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Uploading files in web applications is a common practice. Thanks to php which makes it simple to implement.</p>
<p>This post will present a simple example of how to upload files using php.</p>
<p>Before any code, first let us understand the file upload process .</p>
<p>User is displayed a page with a file select box. She then selects a file and clicks on submit button. The form is submitted now and goes to a php script on the server. There a special array $_FILES gets populated. Similar to $_GET and $_POST, $_FILES contains information about uplaoded files. File information is then extracted from this array and php function move_uploaded_file is used to save file on the server.</p>
<p>Create a new file called upload.php and copy the following code in it.</p>
<pre class="brush:php">
&lt;html&gt;
&lt;head&gt;File upload example&lt;/head&gt;
&lt;body&gt;
&lt;form enctype="multipart/form-data" action="" method="post"&gt;
&lt;input type="hidden" name="MAX_FILE_SIZE" value="300000" /&gt;
&lt;input type="file" name="uploadFile"/&gt;
&lt;br/&gt;
&lt;input type="submit" name="upload" value="Upload File"/&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

&lt;?php
print_r($_FILES); 

if(isset($_POST['upload']))
{
	$folder = 'path/to/folder/';
	$filName = $_FILES['uploadFile']['name'];
	$uploadedFile = $folder.$filName;
	if(is_uploaded_file($_FILES['uploadFile']['tmp_name']))
	{
		 if(move_uploaded_file($_FILES['uploadFile']['tmp_name'],$uploadedFile))
		 {
			echo 'File uploaded';
		 }
		 else
		{
			echo 'Failed to upload file';
		}
	}
	else
	{
		echo 'error';
	}
}
?&gt;	</pre>
<p>We created a form with a hidden field, a file select box and a submit button. After that there is php code which will be executed upon submitting the form.</p>
<p>There are some important points worth noting in the html.</p>
<p><span id="more-229"></span></p>
<ul>
<li><strong>Method specified in form element must be post.</strong></li>
<li><strong>enctype attribute must have the value multipart/form-data &#8211; .</strong> Default value for enctype attribute is application/x-www-form-urlencoded. For file uploads to work, this must be changed to multipart/form-data. This is a common mistake while writing file upload script. So, keep this in mind.</li>
<li><strong>A hidden field with name MAX_FILE_SIZE must be placed before the file select box.</strong> Value for this field should be equal to the maximum size of file that is allowed for upload in bytes. Do not rely on this as it can be altered on the browser. Counter check the size of file on server side to be sure.</li>
</ul>
<p>Now the php code after the user selects a file and clicks submit. As mentioned earlier $_FILES will be filled with information about uploaded file and the selected file will be stored in tmp directory specified in php.ini until we save it to desired location by php code.</p>
<p>Since the name of file select box was uploadFile, $_FILES will contain an associative array with key being uploadFile.</p>
<p>Line 14 will print the contents of $_FILES which look like this:</p>
<pre class="brush:php">
Array
(
    [uploadFile] => Array
        (
            [name] => originalFile.txt
            [type] => application/octet-stream
            [tmp_name] => path/to/tmp_dir/filename.tmp
            [error] => 0
            [size] => 79
        )
)
)</pre>
<p>This array has 5 elements. name is the original name of uploaded file.type is the mime type of file.tmp_name is the name given to the uploaded file in tmp directory. error is the error code returned while uploading the file. 0 means success while uploading and size is the size of uploaded file in bytes.</p>
<p>In line 20, we construct a string which contains the desired path where you wish to upload the file.</p>
<p>Line 21 has the function is_uploaded_file which checks if the file was really uploaded through http post or not.</p>
<p>Now comes the function move_uploaded_file which will move the file from tmp directory to desired path. It accepts 2 parameters: name of the file to upload and the destination path. It returns true on success, false otherwise. In our case first parameter will be the $_FILES['uploadFile']['tmp_name'] and second will be $uploadedFile.</p>
<p>Successful call to move_uploaded_file will move the file from tmp directory to desired path.</p>
<p>Here is the list of possible error code taken from the PHP Manual</p>
<pre class="brush:html">
0- No error, file uploaded.
1- File size is greter then upload_max_filesize value in php.ini.
2- File size is greter then MAX_FILE_SIZE value in HTML form.
3- File partially uploaded.
4- No file uploaded.
6- No temporary folder.
7- Failed writing file to disk.
8- File upload stopped.
</pre>
<p>Comments are most welcome in case of any problem.</p>
<div class="shr-publisher-229"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2008%2F12%2F26%2Ffaq-how-to-upload-files-in-php%2F' data-shr_title='FAQ%3A+How+to+upload+files+in+php%3F'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2008/12/26/faq-how-to-upload-files-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

