<?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>PHP Simple Tutorials &#187; html</title>
	<atom:link href="http://php.elegosproject.org/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://php.elegosproject.org</link>
	<description>Free and user-friendly PHP tutorials</description>
	<lastBuildDate>Sun, 19 Jul 2009 13:12:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Retriving data from HTML forms</title>
		<link>http://php.elegosproject.org/2009/06/17/retriving-data-from-html-forms/</link>
		<comments>http://php.elegosproject.org/2009/06/17/retriving-data-from-html-forms/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 16:46:04 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[Basic]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP / HTML Tutorial]]></category>
		<category><![CDATA[$_GET]]></category>
		<category><![CDATA[$_POST]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://php.elegosproject.org/?p=48</guid>
		<description><![CDATA[Now that we have the basics on how PHP works, let&#8217;s speak on how to get data from a form editable by a common site visitor!

You all know what I&#8217;m talking about. If you don&#8217;t, we&#8217;re going to discover how to get the data sent by the &#8220;comment form&#8221;, where you can leave a comment [...]]]></description>
			<content:encoded><![CDATA[<p>Now that we have the basics on how PHP works, let&#8217;s speak on how to get data from a form editable by a common site visitor!</p>
<p><span id="more-48"></span></p>
<p>You all know what I&#8217;m talking about. If you don&#8217;t, we&#8217;re going to discover how to get the data sent by the &#8220;comment form&#8221;, where you can leave a comment on this article!</p>
<p>Let&#8217;s start from the HTML code. We&#8217;re going to make a form to get a name, an email, a password and a comment (it&#8217;s just an example to explain the various types of data ^^).</p>
<pre class="brush: xml;">&lt;form action=&quot;myphpfile.php&quot; method=&quot;POST&quot;&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;Your name:&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;name&quot; size=&quot;15&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Your password:&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;password&quot; name=&quot;pwd&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Your email:&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;email&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Your comment:&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
	&lt;textarea name=&quot;comment&quot; cols=&quot;40&quot; rows=&quot;5&quot;&gt;&lt;/textarea&gt;&lt;br /&gt;
	&lt;input type=&quot;submit&quot; value=&quot;Send comment&quot; /&gt;
&lt;/form&gt;</pre>
<p>Ok, here we go! As you&#8217;ve seen, I used the &#8220;POST&#8221; method. The action is the page &#8220;myphpfile.php&#8221;. Note that you can use the same PHP page to show the form and to analyze the data. In this case the action will be simply &#8220;?&#8221;. You can use the POST method too, but you&#8217;ll use a different global variable (we&#8217;ll see it in a moment). Since this isn&#8217;t an HTML tutorial, I hope it&#8217;s understandable what I&#8217;ve written since here ^^.</p>
<p>You can send the same data to the same PHP file linking it in this way (you&#8217;ll use the GET method):</p>
<pre class="brush: xml;">&lt;a href=&quot;myphpfile.php?name=Giacomo&amp;pwd=blablabla&amp;email=me@mynet.com&amp;comment=cool&quot;&gt;My link&lt;/a&gt;</pre>
<p>As you can see, the GET method is not designed to send complex data. Also remember that HTML forms (both GET and POST methods) use plain text to send the data, thus I suggest you to implement a java encoder BEFORE the password and/or other sensible data are sent. We may speak about this in another tutorial.</p>
<p>Let&#8217;s see the myphpfile.php lines of code:</p>
<pre class="brush: php;">&lt;?php
	// method: GET --&gt; $_GET['variable_name']
	// the variable name is the &quot;name&quot; tag of the form's input
	$name = $_POST['name'];
	$password = $_POST['pwd'];
	$email = $_POST['email'];
	$comment = $_POST['comment'];

	// let's secure our variables
	$name = htmlspecialchars($name);
	$password = htmlspecialchars($password);
	$email = htmlspecialchars($email);
	$comment = htmlspecialchars($comment);

	if(myLoginFunction($name,$password,$email) == true) echo $comment;
	else echo &quot;Wrong login! You must log in to comment this tutorial!&quot;;
?&gt;</pre>
<p>Here it is the trick: the form you made is able to send data between it and a PHP code. PHP can handle this type of data with the global arrays (that are variables!) $_GET (in this case) and $_POST.</p>
<p><strong>A note about web security: htmlspecialchars()</strong><br />
htmlspecialchars($input) will prevent a user to post malicious code, like JavaScript, PHP or HTML code. This won&#8217;t alter the accented words (àèìòù, äëïöü and so on). This function will change the tags for example from <strong>&gt;</strong> into <strong>?&amp;gt; </strong>which is harmless for the HTML code.<br />
If you want to substitute also accented characters, you&#8217;ll need to use the function htmlentities($input), we can say an &#8220;extension&#8221; of htmlspecialchars() function.</p>
<p>Do you have any question? ^^</p>
]]></content:encoded>
			<wfw:commentRss>http://php.elegosproject.org/2009/06/17/retriving-data-from-html-forms/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
