martin2k

The FORM Tag

HomeForumTutorials Tags:
 
 

Name

Form

Description

Defines an area for a web form on a page.  Controls (elements) such as textboxes, radio buttons etc. are put into this area so that the user can enter data.  At the end of the form, there are usually two buttons - Reset and Submit.  The Reset button resets all the form elements to their default values.  The Submit button sends the contents of the form to the server for processing - usually to a different URL.

The INPUT is the main tag used to define controls.  TEXTAREA is used to define a multi-line textbox.  SELECT is used to define a drop-down list.

ACTION is the only required attribute.

Attributes

ACTION

Specifies the URL to pass the data to.  This could be amongst others, a Perl, PHP or ASP script.

To link to a file on the same site use the local path e.g. action="index.php" or href="folder/contents.php".  You can also use dots to link to a document in the parent folder e.g. href="..\document.asp".  To link to another site, use the full URL e.g.: href="http://www.martin2k.co.uk/cgi-bin/formmail.pl".

METHOD

This needs to be set to either 'get' or 'post'.

If set to 'get', the contents of the form are sent to the URL.  For example, you have a form with one textbox called 'country' and the user types 'England'.  The ACTION attribute is 'result.php'.  When the when the Submit button is pressed, the resultant URL would be:

result.php?country=England

If set to 'post', the contents of the form are passed to the actual page.  An example of this is the form at the end of this page.  The contents of the form are sent to a script called 'formmail.pl', which forwards the contents to an e-mail address.  Usually another URL is specified using an INPUT tag, which the browser is redirected to once the script has received the contents.  In the case of the Comments form below, this is set to 'formthanks.php', which thanks the user for filling in the form.  Different scripts may handle this in a different way.

TARGET

Specifies the target frame for the URL set in 'ACTION'.  As well as name of frames, there are predefined values that can be used to open the page in a specific location:

Value Description
_blank Open in new window
_parent Open in parent frame
_self Open in same frame
_top Open in current window, regardless of frames used

Example

Code

This form will redirect the user to formresult.php, which will display the user's first name in big letters.

<form action="formresult.php" method="get">
First name: <input type="text" name="fname">
<input type="submit" value="Submit" /> <input type="reset" value="Reset">
</form>

Generates

First name:

Please fill in the below form if you have any comments or additional information you want to add about the above information.  If you have any questions, please visit the ForumAny questions sent using this form will be ignored.

* - Mandatory

*Name: E-mail:

*Comments:

*Please type the following code (your comment cannot be accepted without it):


© Martin Allen 1999 - 2006.  Last updated Thursday 26 October 2006 09:35:33 PM +0100.