<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Dealing with Forms</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" 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-2"></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" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="tutorial.useful.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Cap. 2. A simple tutorial</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="tutorial.oldcode.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="tutorial.forms" >Dealing with Forms</A ></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 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 is an example HTML form: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN300" ></A ><P ><B >Exemplu 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"> <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 ></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 write something like this: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN306" ></A ><P ><B >Exemplu 2-7. Printing data from our form</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> Hi <font color="#0000BB"><?php </font><font color="#007700">echo </font><font color="#0000BB">$_POST</font><font color="#007700">[</font><font color="#DD0000">'name'</font><font color="#007700">]; </font><font color="#0000BB">?></font>.<br />You are <font color="#0000BB"><?php </font><font color="#007700">echo </font><font color="#0000BB">$_POST</font><font color="#007700">[</font><font color="#DD0000">'age'</font><font color="#007700">]; </font><font color="#0000BB">?></font> years old.</font> </code></TD ></TR ></TABLE ><P > A sample output of this script may be: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="html" >Hi Joe. You are 22 years old.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > It should be obvious what this does. There is nothing more to it. The <VAR CLASS="varname" >$_POST['name']</VAR > and <VAR CLASS="varname" >$_POST['age']</VAR > variables are automatically set for you by PHP. Earlier we used the <VAR CLASS="varname" >$_SERVER</VAR > autoglobal; 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 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.html" ><B CLASS="function" >import_request_variables()</B ></A > function. </P ><P > 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.html" >short introduction to handling data received from XForms</A > in our features section. </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" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="tutorial.oldcode.html" ACCESSKEY="N" >Înainte</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" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Using old code with new versions of PHP</TD ></TR ></TABLE ></DIV ></BODY ></HTML >