HTML form fields

This assumes you know about HTML forms, the INPUT field and some common attributes and the basics of how forms work.

There are a range of other fields which can be used in forms. The most common are below This leads to an example page in case you get stuck on any of the fields below but try it yourself first. Paste an example of each one into the form created during the HTML forms page above. You may remember that the form you first created sends it's data to a page on this server which then displays your data.

INPUT types

As well as the text type there are other INPUT element types:

Change the type="text" from a form to password and try it. Try the others as well.

Checkbox and radio

These allow the user to enter data just by clicking.

The INPUT type="checkbox" provides an on/off toggle field for the user to say "yes" or "no" quickly. It is possible to have more than one checkbox. The value set for each checkbox is what is sent.

            
<p>What colours do you like?</p>
<p><label for="check1blue">Blue </label><input type="checkbox" name="check1" value="blue" id="check1blue" /></p>
<p><label for="check1green">Green </label><input type="checkbox" name="check2" value="green" id="check1green" /></p>

<p><input type="submit" /></p>                        
        

Paste the code into the page and try it.

The INPUT type="radio" is basically a checkbox but if they are grouped (by having the same name) only one can be selected. This is more or less the same as the last chunk of code:

            
<p>What is your favourite colour?</p>
<p><label for="radio1blue">Blue </label><input type="radio" name="radio1" value="blue" id="radio1blue" /></p>
<p><label for="radio1green">Green </label><input type="radio" name="radio1" value="green" id="radio1green" /></p>        
<p><input type="submit" /></p>                        
        

The radio elements have different IDs to allow use of labels, JavaScript and CSS.

SELECT and OPTION (and OPTGROUP)

A SELECT field is a drop down list of options. The user can select one or more:

            
<form method="get" action="fred.php">
    <select name="colour">
    <option>blue</option>
    <option selected>green</option>
    <option>red</option>
    <option>pink</option>
    <option>black</option>
    </select>
    <input type="submit" />
</form>                        
        

Paste the code into the page and try it.

To have one item in the list automatically selected by default use the SELECT attribute (green is selected above).

If no VALUE is set then the text inside the OPTION tags is sent to the server instead.

It is also possible to allow the user to select multiple entries in the list and to have more than one item shown:

        
<form method="get" action="fred.php">
    <select name="colour" size="4" multiple>
        <option>blue</option>
        <option selected>green</option>
        <option>red</option>
        <option>pink</option>
        <option>black</option>
    </select>
    <input type="submit" />
</form>
        

To select more than one item the user needs to use the control key on their keyboard.

OPTGROUP is used to organise the options in a SELECT list:

        
<form method="get" action="fred.php">
    <select name="colour">
        <optgroup label="pretty colours">
            <option>blue</option>
            <option selected>green</option>
            <option>red</option>
        </optgroup>
        <optgroup label="nasty colours">
            <option>pink</option>
            <option>black</option>
        </optgroup>
    </select>
    <input type="submit" />
</form>
        

Text area

This allows the user to type in a bigger area and include carriage returns. It does not allow formatting:

            
<textarea name="essay" rows="20" cols="50"></textarea>                        
        

Anything within the two TEXTAREA tags appears as a default value in the field. Add it to a form.

Results

Things to note when creating a form:

You should be able to see all of that in your form page when you submit it.

Layout

By default form fields are in-line elements and that is why the submit button in this example is on the same line as the text INPUT.

The fields could be put inside P tags for simple layout but often a complex form needs more than that. Nested DIVs would allow the labels and fields to be lined up nicely. Or perhaps a DIV for the labels and another floated alongside it for the fields. Take care what happens with different sized fonts (for accessibility) or when the browser window changes size.

Have a go at laying out your form fields nicely. Look at some forms on the Web for ideas if you need them.

submit to reddit Delicious Tweet