Forms
Last edited by admin Wed, 10/03/2012 - 20:06

 

What HTML forms do

Once you master forms, you will be able to do receive user input. In the majority of cases forms are used together with a separate PHP or CGI script. Using HTML you determine how a feedback or registration page will look like; the dynamic scripts process the provided information. In order for a web form to work, both its parts -- HTML and script, must be done properly, which means that the HTML code has to be correct, and the server needs to be configured to run scripts.

Creating forms

The form itself is an area that contains form elements. It has the following basic syntax:

<form>
  <input ...>
</form>

Input types

Different input types are used in different situations.

Text field

Text fields are used when the user has to type some information.

<form>
Name:
<input type="text" name="my_name">
</form>

The above code renders as:

 

Name:

Checkbox

<form>
I like pie:
<input type="checkbox" name="taste" value="pie" />
<br />
I like chocolate:
<input type="checkbox" name="taste" value="chocolate" />
</form>

The above code renders as:

 

I like pie:


I like chocolate:

Several options can be selected from a list.

Radio button

Unlike checkboxes, radio buttons allow for choosing one option only.

<form>
<input type="radio" name="opinion" value="agree"> I agree.
<br>
<input type="radio" name="opinion" value="disagree"> I disagree.
</form>

I agree.


I disagree.

The submit button

Most often, the script associated with an HTML form is invoked with a "Submit" button. Pressing the button sends the information from the form to another file, which then processes it.


<form name="input" action="mail.php"
method="get">
Enter email to subscribe to our newsletter:
<input type="text" name="subscribe">
<input type="submit" value="Subscribe">
</form>

method="get">
Enter email to subscribe to our newsletter: