2 hours
HTML Form is one of the most popular ways to gather user information and send it to an application. It is the most efficient way to let the user enter the data. For example, it can be used to collect things like a name and email in a sign-up form. When the user/visitor enters the details in the form, it is posted to the backend server where the processing is done and then the necessary data is stored in the database. There are various elements which can be used in the form which are as follows: 1. Input element : It is the most commonly used field in the form which allows the user to specify different types of values. It can be text field, number field, password field, checkbox etc.
Following is an example which shows the code snippet to declare the input element.
<form>
<label for="name">Name:</label>
<input type="text" name="name" id="name">
</form>
<label>
tag should be equal to the id attribute of the <input>
element to bind them together.<input>
element.Following is an example which shows the code snippet when the input type is checkbox.
<form>
<input type="checkbox" id="apple" name="apple" value="Apple">
<label for="apple">Apple</label>
<input type="checkbox" id="mango" name="mango" value="Mango">
<label for="mango">Mango</label>
<input type="checkbox" id="banana" name="banana" value="Banana">
<label for="banana">Banana</label>
</form>
Another important type of input field is date. This field allows user to enter only date Following is an example which shows the code snippet when the input type is date.
<form>
<label for="dob">DOB:</label>
<input type="date" id="dob" name="dob">
</form>
Following is an example which shows the code snippet to declare the Select element.
<form>
<select id="fruits" name="fruits">
<option value="apple">Apple</option>
<option value="mango">Mango</option>
<option value="banana">Banana</option>
</select>
</form>
Following is an example which shows the code snippet to declare the Textarea Element.
<form>
<label for="message">Message:</label>
<textarea rows="2" cols="30" name="message" id="message"></textarea>
</form>
Following is an example which shows the code snippet to declare the Button Element.
<form>
<button type="button">Click Me!</button>
</form>
There can be different types of buttons which are as follows:
submit: This type of button sends the data entered by the user to the server.
reset: The button resets all the controls to their initial values.
button: The button has no default behavior, and does nothing when pressed by default.
HTML elements which allows restriction in the form elements are the validation attribute. 1. Readonly Attribute : This attribute do not allow any text to be entered in the input field.
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="Shreya" readonly>
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="shreyakapoor98@gmail.com">
</form>
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="Shreya" disabled>
</form>
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name" size="40">
</form>
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</form>
<form>
<label for="contact">Contact:</label>
<input type="number" id="contact" name="contact" maxlength="10">
</form>
<form>
<label for="contact">Contact:</label>
<input type="number" id="contact" name="contact" minlength="8">
</form>
<form>
<label for="docs">Select Document:</label>
<input type="file" id="docs" name="docs" multiple>
</form>
<form>
<label for="marks">Marks:</label>
<input type="number" id="marks" name="marks" step="3">
</form>
Submit : When the user enters the details which are asked, the data or the information is passed for further processing. Input field of type submit is taken to submit the data.
Following code, the snippet shows the illustration of the submission of the form.
<form action="/submit_action.php" method="get">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="address">Address:</label>
<input type="text" id="address" name="address">
<input type="submit" value="Submit">
</form>
In the above example the form data is sent to a page on the server called “/submit_action.php”. This page contains a server-side script that handles the form data and the method is GET which will send the data as a URL parameter.
Spend 15 minutes researching these topics online: - Learn about what happens when an HTML form is submitted. - Sending Form Data By Mozilla - HTML Forms By w3schools
Spend 15 minutes to get as far as you can on this quiz to check your understanding of forms.