Working with forms
Working with forms
HTML Forms
• An HTML form is used to collect user input. The user input is most
often sent to a server for processing.
• The <form> Element
• The HTML <form> element is used to create an HTML form for user
input:
• <form>
.
form elements
.
</form>
• The <form> element is a container for different types of input
elements, such as: text fields, checkboxes, radio buttons, submit
buttons, etc.
• The <input> Element
• The HTML <input> element is the most used form element.
• An <input> element can be displayed in many ways, depending on the
type attribute.
Type Description
<input type="text"> Displays a single-line text input field
<input type="radio"> Displays a radio button (for selecting one
of many choices)
<input type="checkbox"> Displays a checkbox (for selecting zero or
more of many choices)
<input type="submit"> Displays a submit button (for submitting
the form)
<input type="button"> Displays a clickable button
Form Attributes
• The Action Attribute
• The action attribute defines the action to be performed when the form is
submitted.
• <form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
The Target Attribute
The target attribute specifies where to display the response that is received after submitting the form.
Value Description
• The input disabled attribute specifies that an input field should be disabled.
• A disabled input field is unusable and un-clickable.
• The value of a disabled input field will not be sent when submitting the
form!
• <form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John" disabled><br
>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe">
</form>
The size Attribute
• The input multiple attribute specifies that the user is allowed to enter
more than one value in an input field.
• The multiple attribute works with the following input types: email,
and f
• <form>
<label for="files">Select files:</label>
<input type="file" id="files" name="files" multiple>
</form> ile.
The placeholder Attribute
• The input required attribute specifies that an input field must be filled
out before submitting the form.
• <form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
</form>
The autofocus Attribute
<form>
First name:
<input type="text" id="fname" name="fname"><
br>
Last name:
<input type="text" id="lname" name="lname">
</form>