HTML Forms
HTML Forms
Apr 8, 2015
For example, you might want to check that a zipcode field contains a 5-digit
integer before you send that information to the server
action="url"
(default)
Form data is sent as a URL with ?form_data info appended to the end
Can be used only if data is all ASCII and not more than 100 characters
method="post"
Specifies where to send the data when the Submit button is clicked
method="get"
(required)
target="target"
Most, but not all, form elements use the input tag, with a
type="..." argument to tell which kind of element it is
Text input
A text field:
<input type="text" name="textfield" value="with an initial value" />
A password field:
<input type="password" name="textfield3" value="secret" />
Notethattwooftheseusetheinputtag,butoneusestextarea
6
Buttons
A submit button:
<input type="submit" name="Submit" value="Submit" />
A reset button:
<input type="reset" name="Submit2" value="Reset" />
A plain button:
<input type="button" name="Submit3" value="Push Me" />
Radio buttons
Radio buttons:<br>
<input type="radio" name="radiobutton" value="myValue1" />
male<br>
<input type="radio" name="radiobutton" value="myValue2
checked="checked" />female
If two or more radio buttons have the same name, the user can
only select one of them at a time
If you ask for the value of that name, you will get the value
specified for the selected radio button
As with checkboxes, radio buttons do not contain any text
8
Labels
In many cases, the labels for controls are not part of the control
In my testing (Firefox and Opera), this isnt necessary, but it may be for
some browsers
Checkboxes
A checkbox:
<input type="checkbox" name="checkbox"
value="checkbox" checked="checked">
type: "checkbox"
name: used to reference this form element from JavaScript
value: value to be returned when element is checked
Note that there is no text associated with the checkbox
Unless you use a label tag, only clicking on the box itself has any
effect
10
A menu or list:
<select name="select">
<option value="red">red</option>
<option value="green">green</option>
<option value="BLUE">blue</option>
</select>
Additional arguments:
if set to "true" (or just about anything else), any number of items may be
selected
if omitted, only one item may be selected
if set to "false", behavior depends on the particular browser
11
Hidden fields
All input fields are sent back to the server, including hidden fields
This is a way to include information that the user doesnt need to see (or
that you dont want her to see)
The value of a hidden field can be set programmatically (by JavaScript)
before the form is submitted
12
A complete example
<html>
<head>
<title>Get Identity</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<p><b>Who are you?</b></p>
<form method="post" action="">
<p>Name:
<input type="text" name="textfield">
</p>
<p>Gender:
<label><input type="radio" name="gender" value="m" />Male<label>
<label><input type="radio" name="gender" value="f" />Female</label>
</p>
</form>
</body>
</html>
13
The End
14