Source of: /manual/en/tutorial.forms.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/tutorial.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'tutorial.forms.php',
1 => 'Dealing with Forms',
),
'up' =>
array (
0 => 'tutorial.php',
1 => 'A simple tutorial',
),
'prev' =>
array (
0 => 'tutorial.useful.php',
1 => 'Something Useful',
),
'next' =>
array (
0 => 'tutorial.oldcode.php',
1 => 'Using old code with new versions of PHP',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="tutorial.forms" class="section">
<div class="info"><h1 class="title">Dealing with Forms</h1></div>
<p class="para">
One of the most powerful features of PHP is the way it handles HTML
forms. The basic concept that is important to understand is that any
form element will automatically be available to your PHP
scripts. Please read the manual section on
<a href="language.variables.external.php" class="link">Variables from external
sources</a> for more information and examples on using forms
with PHP. Here is an example HTML form:
</p>
<p class="para">
</p><div class="example">
<div class="info"><p><b>Example #1 A simple HTML form</b></p></div>
<div class="example-contents programlisting">
<div class="htmlcode"><pre class="htmlcode"><form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form></pre>
</div>
</div>
</div><p>
</p>
<p class="para">
There is nothing special about this form. It is a straight HTML form
with no special tags of any kind. When the user fills in this form
and hits the submit button, the <var class="filename">action.php</var> page
is called. In this file you would write something like this:
</p>
<p class="para">
</p><div class="example">
<div class="info"><p><b>Example #2 Printing data from our form</b></p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
Hi <span style="color: #0000BB"><?php </span><span style="color: #007700">echo </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]); </span><span style="color: #0000BB">?></span>.<br />You are <span style="color: #0000BB"><?php </span><span style="color: #007700">echo (int)</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'age'</span><span style="color: #007700">]; </span><span style="color: #0000BB">?></span> years old.</span>
</code></div>
</div>
<div class="example-contents para"><p>
A sample output of this script may be:
</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Hi Joe. You are 22 years old.
</pre></div>
</div>
</div><p>
</p>
<p class="para">
Apart from the <a href="function.htmlspecialchars.php" class="function">htmlspecialchars()</a> and
<i>(int)</i> parts, it should be obvious what this does.
<a href="function.htmlspecialchars.php" class="function">htmlspecialchars()</a> makes sure any characters that are
special in html are properly encoded so people can't inject HTML tags
or Javascript into your page. For the age field, since we know it is a
number, we can just <a href="language.types.type-juggling.php#language.types.typecasting" class="link">convert</a>
it to an <a href="language.types.integer.php" class="type integer">integer</a> which will automatically get rid of any
stray characters. You can also have PHP do this for you automatically by
using the <a href="ref.filter.php" class="link">filter</a> extension.
The <var class="varname"><a href="reserved.variables.post.php" class="classname">$_POST['name']</a></var> and <var class="varname"><a href="reserved.variables.post.php" class="classname">$_POST['age']</a></var>
variables are automatically set for you by PHP. Earlier we
used the <var class="varname"><a href="reserved.variables.server.php" class="classname">$_SERVER</a></var> superglobal; above we just
introduced the <var class="varname"><a href="reserved.variables.post.php" class="classname">$_POST</a></var>
superglobal which contains all POST data. Notice how the
<em class="emphasis">method</em> of our form is POST. If we used the
method <em class="emphasis">GET</em> then our form information would live in
the <var class="varname"><a href="reserved.variables.get.php" class="classname">$_GET</a></var> superglobal instead.
You may also use the <var class="varname"><a href="reserved.variables.request.php" class="classname">$_REQUEST</a></var>
superglobal, if you do not care about the source of your request data. It
contains the merged information of GET, POST and COOKIE data. Also see the
<a href="function.import-request-variables.php" class="function">import_request_variables()</a> function.
</p>
<p class="para">
You can also deal with XForms input in PHP, although you will find yourself
comfortable with the well supported HTML forms for quite some time.
While working with XForms is not for beginners, you might be interested
in them. We also have a <a href="features.xforms.php" class="link">short introduction
to handling data received from XForms</a> in our features section.
</p>
</div><?php manual_footer(); ?>