<HTML ><HEAD ><TITLE >Dealing with Forms</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="A simple tutorial" HREF="tutorial.html"><LINK REL="PREVIOUS" TITLE="Something Useful" HREF="tutorial.useful.html"><LINK REL="NEXT" TITLE="Using old code with new versions of PHP" HREF="tutorial.oldcode.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="tutorial.useful.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 2. A simple tutorial</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="tutorial.oldcode.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="tutorial.forms" ></A >Dealing with Forms</H1 ><P > 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 in a form will automatically be available to your PHP scripts. Please read the manual section on <A HREF="language.variables.external.html" >Variables from outside of PHP</A > for more information and examples on using forms with PHP. Here's an example HTML form: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN272" ></A ><P ><B >Example 2-6. A simple HTML form</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" ><form action="action.php" method="POST"> Your name: <input type="text" name="name" /> Your age: <input type="text" name="age" /> <input type="submit"> </form></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 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 <TT CLASS="filename" >action.php</TT > page is called. In this file you would have something like this: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN278" ></A ><P ><B >Example 2-7. Printing data from our form</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Hi <?php echo $_POST["name"]; ?>. You are <?php echo $_POST["age"]; ?> years old.</PRE ></TD ></TR ></TABLE ><P > A sample output of this script may be: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" >Hi Joe. You are 22 years old.</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P > It should be obvious what this does. There is nothing more to it. The <TT CLASS="varname" >$_POST["name"]</TT > and <TT CLASS="varname" >$_POST["age"]</TT > variables are automatically set for you by PHP. Earlier we used the <TT CLASS="varname" >$_SERVER</TT > autoglobal, now above we just introduced the <A HREF="reserved.variables.html#reserved.variables.post" >$_POST</A > autoglobal which contains all POST data. Notice how the <SPAN CLASS="emphasis" ><I CLASS="emphasis" >method</I ></SPAN > of our form is POST. If we used the method <SPAN CLASS="emphasis" ><I CLASS="emphasis" >GET</I ></SPAN > then our form information would live in the <A HREF="reserved.variables.html#reserved.variables.get" >$_GET</A > autoglobal instead. You may also use the <A HREF="reserved.variables.html#reserved.variables.request" >$_REQUEST</A > autoglobal if you don't care the source of your request data. It contains a mix of GET, POST, COOKIE and FILE data. See also the <A HREF="function.import-request-variables.html" ><B CLASS="function" >import_request_variables()</B ></A > function. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="tutorial.useful.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="tutorial.oldcode.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Something Useful</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="tutorial.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Using old code with new versions of PHP</TD ></TR ></TABLE ></DIV ></BODY ></HTML >