36
loading...
This website collects cookies to deliver better user experience
<form></form>
Form Tag - This is form tag used to make forms. But, this is like a container or wrapper for your input fields. But it's has some very important attributes that you should know.Attributes | Values | Description |
---|---|---|
method | get| post | It's specify how to transfer data from site to the site specified in `action` attribute. |
action | (path of HTML file or server) | It's specify, where to redirect user after filling form |
Value | Description |
---|---|
get | This append the form data in the URL (https://site.com?name=modern+web) |
post | Sends the form data as HTTP post transaction. This is more secure way to transfer data than `get` |
action
and method
example at the last of the post where we'll make google search engine. Isn't it excited.<input>
Input tag. This tag is self closing, it does not require a closing tag. Input Tag is used to add fields in our form. Let's see them in detail.Atrribute | Value | Description |
---|---|---|
type | text, email, password, radio, checkbox, submit, range, date, color, file, reset, and more | Use to specify what type of input it is. |
name | text. Example `name="first name"` | It is used to specify name for the input, so we can identify data |
placeholder | text. Example `placeholder="type your first name"` | Specifies a short hint that describes the expected value of an element |
value | text. Example `value="john"` | it is used to specify the default value for the input |
required | --- | It specify the field must be filled before submitting form. |
type
attribute is used to specify input type.<form>
<input type="text" placeholder="type your name" name="name">
</form>
we are not using method and action attributes in above example because we don't have submit button.
Email type is same as text but email text check @
sign also.
<form>
<input type="password" placeholder="password" name="password">
</form>
<form>
<input type="range" name="range" min="0" max="10" step="2" value="4">
</form>
<form>
<input type="radio" id="red" name="color" value="red" checked>
<label for="red">red</label>
<input type="radio" name="color" id="green" value="green">
<label for="green">green</label>
<input type="radio" name="color" id="blue" value="blue">
<label for="blue">blue</label>
<input type="radio" name="color" id="yellow" value="yellow">
<label for="yellow">yellow</label>
</form>
<label></label>
Label tag. It is used for input fields to add a label to them. In the above case we are adding label to our radio buttons.id
attribute. id
is used to give unique ID to any element.for
attribute. for
attribute is for label tag. It specifies for which input this label is. The value of this for
attribute should be same to the id
of the element.<form>
<input type="checkbox" id="red" name="color" value="red" checked>
<label for="red">red</label>
<input type="checkbox" name="color" id="green" value="green">
<label for="green">green</label>
<input type="checkbox" name="color" id="blue" value="blue">
<label for="blue">blue</label>
<input type="checkbox" name="color" id="yellow" value="yellow">
<label for="yellow">yellow</label>
</form>
name
. Same name
means it belongs to same category.<form>
<input type="date">
</form>
<form>
<!-- to add color field in the form -->
<input type="color">
<br>
<br>
<!-- to add upload file -->
<input type="file">
<br>
<br>
<!-- to add a reset button which reset all input data -->
<input type="reset" value="reset">
<br>
<br>
<!-- to add submit button -->
<input type="submit" value="submit">
</form>
action
attribute.<form method="get" action="https://www.google.com/search?">
<label for="search">Search</label>
<input type="text" id="search" name="q" required>
<input type="submit" value="search">
</form>
method
to get
so it will append form data into URL.action
attribute we have given a link https://www.google.com/search?
. But from where I got this link.
action="https://www.google.com/search?q=car&sxsrf=ALeKk00oK8YP9RI6P3t-9YfvtPoZwqXsng%3A1627294553711...."
it's a very long address.car
in search box. And if we see the link. We can find our car word here q=car
. So okay, means google want this q field atleast to work.action
is equal to https://www.google.com/search?
. We also removed that q
word. But remember to add ?
at last.name="q"
. Why? because google want this q
field. And whatever we type in our field. It data will belong to q
field. <button></button>
Button tags also. It is used to create button in the page. And you can use These buttons as submit button you only have to add type="submit"
.<button>Click me</button>