<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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',
    2 => '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.whatsnext.php',
    1 => 'What\'s next?',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'chapters/tutorial.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<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">
    <div class="example" id="example-1">
     <div class="info"><p><strong>Example #1 A simple HTML form</strong></p></div>
     <div class="example-contents">
<div class="htmlcode"><pre class="htmlcode">&lt;form action=&quot;action.php&quot; method=&quot;post&quot;&gt;
    &lt;label for=&quot;name&quot;&gt;Your name:&lt;/label&gt;
    &lt;input name=&quot;name&quot; id=&quot;name&quot; type=&quot;text&quot;&gt;

    &lt;label for=&quot;age&quot;&gt;Your age:&lt;/label&gt;
    &lt;input name=&quot;age&quot; id=&quot;age&quot; type=&quot;number&quot;&gt;

    &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
&lt;/form&gt;</pre>
</div>
     </div>

    </div>
   </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">
    <div class="example" id="example-2">
     <div class="info"><p><strong>Example #2 Printing data from our form</strong></p></div>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">Hi <span style="color: #0000BB">&lt;?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">?&gt;</span>.<br />You are <span style="color: #0000BB">&lt;?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">?&gt;</span> years old.</span></code></div>
     </div>

     <div class="example-contents"><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 class="para">
    Apart from the <span class="function"><a href="function.htmlspecialchars.php" class="function">htmlspecialchars()</a></span> and
    <code class="literal">(int)</code> parts, it should be obvious what this does.
    <span class="function"><a href="function.htmlspecialchars.php" class="function">htmlspecialchars()</a></span> makes sure any characters that are
    special in html are properly encoded so people can&#039;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 <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> 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>method</em> of our form is POST.  If we used the
    method <em>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.
   </p>
  </div><?php manual_footer($setup); ?>