HTML FORMS
Introduction
Syntax for <form >:
<form action=”Script URL” method =”GET| POST”>
Form elements like buttons, text box etc.,
</form>
Method Attribute
This attribute specifies how the form will submit the data to the address specified by the action attribute.
There are two possible values for method attribute.
GET:
POST:
From Elements
Example of Form Elements
Attributes of the input tag
Label
The content of the <label> tag is a piece of ordinary text.
It is used to add a label to form field.
It describe meaning of the associated form field.
<label> form_content... </label>
Textfield
Name: name of the textfield.
Size: indicates size of th text.
Value: indicates default value of the text.
Readonly: indicates the text field information can not be modified.
Maxlength: allows the user to enter text of maximum length.
Align: indicates the alignment of the text field. It might be left, right , middle.
<!DOCTYPE html>
<html>
<body>
<h2>Text field</h2>
<form >
<label >First name:</label><br>
<input type="text" name="fname"><br>
<label >Last name:</label><br>
<input type="text" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Password
Note: characters entered are not encrypted, they are only shown as asterisks or dots.
Password Field Example
<!DOCTYPE html>
<html>
<body>
<h2>Text field</h2>
<form>
<label>Username:</label><br>
<input type="text" name="username"><br>
<label >Password:</label><br>
<input type="password" name="pwd">
</form>
</body>
</html>
Check Box
A checkbox is like a toggle switch i.e, it can be in either of the two states checked or unchecked.
Checkboxes allow visitors to select one or more options from the set of related alternatives.
A check box is created using < input > tag and specifying the type=”checkbox”.
CheckBox
<!DOCTYPE html>
<html>
<body>
<h2>Check Box</h2>
<form>
<input type="checkbox" name="vehicle1" value="Bike">
<label > I have a bike</label><br>
<input type="checkbox" name="vehicle2" value="Car">
<label > I have a car</label><br>
<input type="checkbox" name="vehicle3" value="Boat">
<label > I have a boat</label>
</form>
</body>
</html>
Value attribute stores the value to be returned when the checkbox is checked.
Radio Button
<!DOCTYPE html>
<html>
<body>
<h2>Radio Button</h2>
<p>Choose your favorite Web language:</p>
<form>
<input type="radio" value="HTML">
<label >HTML</label><br>
<input type="radio" value="CSS">
<label >CSS</label><br>
<input type="radio" value="JavaScript">
<label >JavaScript</label>
</form>
</body>
</html>
Selection List or DropDown Lists
<!DOCTYPE html>
<html>
<body>
<h2>Selection List</h2>
<form>
<label>Choose your Favorite city in India</label>
<select>
<option>New Delhi</option>
<option>Indore</option>
<option>Jaipur</option>
<option>Jodhpur</option>
<option>Chandigarh</option>
<option>Mumbai</option>
<option>Bengaluru</option>
<option>Lucknow</option>
<option>Amritsar</option>
</select>
</form>
</body>
</html>
TEXT AREA
Syntax:
<textarea rows=4 cols=20>
----------
</textarea>
Example
<!DOCTYPE html>
<html>
<body>
<h2>Text Area</h2>
<textarea rows=4 cols=20>
enter your text here
</textarea>
</body>
</html>
File upload
Example
<!DOCTYPE html>
<html>
<body>
<h2>File Upload</h2>
<form action="upload" method="post">
Select a file(.pdf)<br>
<input type="file" name="file"> <br>
</form>
</body>
</html>
Note:
BUTTON
Ex:
<input type=”button” value=” Logout”>
<button> Logout </button>
Action Buttons
Two types of buttons we use almost for all forms are:
Submit button:
Submit button is used to send the url to the server script whose URL is specified by the “action” attribute of the form tag.
<input type="submit" value="Send Request">
<!DOCTYPE html>
<html>
<body>
<h2>Button</h2>
<form >
<input type="submit" value="Send Request">
</input>
</form>
</body>
</html>
WILL CREATE SUBMIT BUTTON WITH TEXT ON BUTTON AS “Send Request”, which when clicked, the form response will be submitted.
RESET Button
<input type="reset" value="Restore Default Values">
<!DOCTYPE html>
<html>
<body>
<h2>Button</h2>
<form >
<input type="reset" value="Restore Default Values">
</input>
</form>
</body>
</html>
Attribute type specifies that it is a reset button and value specifies that text to be displayed on the reset button.
<!DOCTYPE html>
<html>
<body>
<h2>Button</h2>
<form >
<input type="reset" value="Restore Default Values"
onclick="return confirm('Do you really want to reset the form?')">
</input>
</form>
</body>
</html>
In this case , reset button is clicked, a dialog box appears for confirmation.
So, if the user clicks reset button accidentally the user may have chance to cancel it.
Image Button
<!DOCTYPE html>
<html>
<body>
<h2>Button</h2>
<form >
<input type="image" src="Sample.jpg">
</input>
</form>
</body>
</html>