HTML Code Tutorial

Tutor Ebook Download

Using Check boxes in Forms

Check boxes allow your user to make more than one selection in a list.
The Tag:

Label <input type="checkbox" name="" value="">

Attributes

name
Usually refers to the name of the variable where the value entered on the form is to be stored.
value
Refers to the information or data entered or selected on the form.
checked
Displays a check mark in the desired input box. (See Code Example)

Example:

Red <input type="checkbox" name="color" value="red">
Green <input type="checkbox" name="color" value="green">
Blue <input type="checkbox" name="color" value="blue">

When using check boxes the name attribute must be the same for each check box.

In the example above, multiple check boxes would each share the name color.

In the example above multiple check boxes would include a different color in each value attribute.

The Label preceding or following the check box would match the value.

Now let's build a simple form:

Check Your Favorite Pass Times:
Watching TV
Hiking
Surfing the net.
Building Web Pages.
Reading a book.
Playing Games.


Note: Try the form. Execution is via PHP. (Script below)
<form method="post" action="script name">
<b>Check Your Favorite Pass Times:</b><br>
<input type="checkbox" name="passtime" value="TV">Watching TV<br>
<input type="checkbox" name="passtime" value="Hiking">Hiking<br>
<input type="checkbox" name="passtime" value="Surfing">Surfing the net.<br>
<input type="checkbox" name="passtime" value="Building" checked>Building Web Pages.<br>
<input type="checkbox" name="passtime" value="Reading">Reading a book.<br>
<input type="checkbox" name="passtime" value="Games">Playing Games.<br>
<input type="submit" value="Submit"><br>
</form>

The Code:

Take a quiz. Take a Quiz on this Information.


Execute Form With PHP

PHP scripts don't have to be in a special folder. They can be placed in the same directory or folder with your form. Here's the simple script that processes the form example above.


<html>
<head><title></title></head>
<body>
<?
print "<h1>Your Choices</h1>";
foreach ($passtime as $value){
print "$value<br>";
}
?>
</body>
</html>

Execute Form With PERL

Note: Copy this code and paste it into a page. Copy the script demo_html40.pl and place it in your cgi-bin folder. Then start the server and run the page that contains the checkbox form.



CGI Online
How to Create a Website -Tutorial
Main Website


This Free HTML Tutorial
is provided by Net Success 2000 Plus Inc.
PO Box 1508
Somerset, KY 42502
Last Modified: September 3, 2007

HTML Codes Tutor Ebook