Lecture 4 Notes (2) - 3999
Lecture 4 Notes (2) - 3999
<form> tag
Basic Structure:
● ‘action’: Specifies the URL or script to which the form data should be
submitted. It indicates where the form data will be processed.
● ‘method’: Defines the HTTP method to send form data to the server.
Typical values are "get" and "post".
● ‘enctype’: Specifies the encoding type for form data when using the
post method. Common values include
"application/x-www-form-urlencoded" and
"multipart/form-data". This is often used when dealing with file
uploads.
Example:
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
In this example, the form collects a username and password, and when
submitted, the data is sent to the server-side script specified in the action
attribute. The method is set to "post" for more secure data transmission,
and the enctype is specified for potential file uploads. The required
attribute ensures that users must fill in the specified fields before
submitting the form.
● ‘label’: The <label> tag associates a text label with a form control,
enhancing accessibility and user experience. It is often used with
form elements like <input>, <select>, and <textarea>.
<label for="username">Username:</label>
<input type="text" id="username" name="username">
● ‘<select> and <option>’: The <select> tag creates a dropdown list,
and <option> tags define the options within the list.
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<button type="submit">Submit</button>
<fieldset>
<legend>Personal Information</legend>
<!-- Form elements go here -->
</fieldset>
<form oninput="result.value=parseInt(a.value)+parseInt(b.value)">
<input type="range" id="a" value="50">+
<input type="number" id="b" value="50">
=<output name="result" for="a b">100</output>
</form>
<select>
<optgroup label="Fruits">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
</optgroup>
<optgroup label="Vegetables">
<option value="carrot">Carrot</option>
<option value="broccoli">Broccoli</option>
</optgroup>
</select>
These HTML tags play crucial roles in creating interactive and user-friendly
forms on web pages. They provide a variety of input methods and
customisation options to cater to different types of user interactions.
HTML Input Types
Here are the different input types you can use in HTML:
Each input type serves a specific purpose, providing a wide range of options
for collecting user input in web forms.
File Input
The <input> element with type="file" is used to create a file input field,
allowing users to select and upload files from their devices. The associated
attributes -accept, -capture, -id, and -name are not standard HTML
attributes for the <input> element, but they might be used in certain
contexts or frameworks.
Here is the explanation of the standard and commonly used attributes for
the <input type="file"> element:
1. accept: Specifies the types of files the file input should accept. It is a
comma-separated list of file type specifiers, such as file extensions or
MIME types. This attribute can help filter the types of files that users
can select.
3. id: Specifies a unique identifier for the input element. This can be
useful for associating the input with a label or for targeting the
element with JavaScript or CSS.
4. name: Specifies the input element's name, which is sent to the server
when the form is submitted. This attribute is crucial for identifying
the input field in server-side processing.
Button Tag
<button type="submit">Submit</button>
3. value: Assigns a value to the button, which is also sent to the server
when the form is submitted. Useful when multiple buttons submit
the same form.
2. formenctype: Specifies the encoding type for form data when the
form is submitted. It can take values such as
"application/x-www-form-urlencoded", "multipart/form-data", or
"text/plain".
Note: This attribute overrides the enctype attribute of the <form>
element.
CONCLUSION:
In conclusion, the attributes and elements discussed in the context of HTML
forms provide developers with a powerful toolkit to create interactive and
customisable user interfaces. Starting with the <form> tag as the
foundational element, various attributes such as action, method, and
enctype define how form data is submitted and processed on the server.
Attributes like autocomplete, novalidate, and target offer control over the
form's behaviour and user experience.
REFERENCES: