Html forms

22
HTML :: FORMS www.eshikshak.co.in

description

HTML Forms are used to select different kinds of user input.

Transcript of Html forms

Page 1: Html forms

HTML :: FORMSwww.eshikshak.co.in

Page 2: Html forms

IntroductionIntroduction

HTML Forms are required when you want to HTML Forms are required when you want to collect some data from the site visitor. For example collect some data from the site visitor. For example registration information: name, email address, registration information: name, email address, credit card, etc.credit card, etc.

Form elements are like text fields, textarea fields, Form elements are like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc. drop-down menus, radio buttons, checkboxes, etc. which are used to take information from the user.which are used to take information from the user.

www.eshikshak.co.in

Page 3: Html forms

<form action="back-end script" method="posting method"> form elements like input, textarea etc.</form>

www.eshikshak.co.in

Page 4: Html forms

Most frequently used form attributes Most frequently used form attributes

name:name: This is the name of the form. This is the name of the form. action:action: Here you will specify any script URL which will receive uploaded Here you will specify any script URL which will receive uploaded

data.data. method:method: Here you will specify method to be used to upload data. It can Here you will specify method to be used to upload data. It can

take various values but most frequently used are GET and POST.take various values but most frequently used are GET and POST. target:target: It specifies the target page where the result of the script will be It specifies the target page where the result of the script will be

displayed. It takes values like _blank, _self, _parent etc.displayed. It takes values like _blank, _self, _parent etc. enctype:enctype: You can use the enctype attribute to specify how the browser You can use the enctype attribute to specify how the browser

encodes the data before it sends it to the server. Possible values are like:encodes the data before it sends it to the server. Possible values are like: application/x-www-form-urlencodedapplication/x-www-form-urlencoded - This is the standard method most - This is the standard method most

forms use. It converts spaces to the plus sign and non-alphanumeric characters forms use. It converts spaces to the plus sign and non-alphanumeric characters into the hexadecimal code for that character in ASCII text.into the hexadecimal code for that character in ASCII text.

mutlipart/form-datamutlipart/form-data - This allows the data to be sent in parts, with each - This allows the data to be sent in parts, with each consecutive part corresponding the a form control, in the order they appear in consecutive part corresponding the a form control, in the order they appear in the form. Each part can have an optional content-type header of its own the form. Each part can have an optional content-type header of its own indicating the type of data for that form control.indicating the type of data for that form control.

www.eshikshak.co.in

Page 5: Html forms

Most frequently used form attributes Most frequently used form attributes

There are different types of form controls that you There are different types of form controls that you can use to collect data from a visitor to your site.can use to collect data from a visitor to your site. Text input controlsText input controls ButtonsButtons Checkboxes and radio buttonsCheckboxes and radio buttons Select boxesSelect boxes File select boxesFile select boxes Hidden controlsHidden controls Submit and reset buttonSubmit and reset button

www.eshikshak.co.in

Page 6: Html forms

Single-line text input controlsSingle-line text input controls

Single-line text input controls are created using an <input> Single-line text input controls are created using an <input> element whose type attribute has a value of text.element whose type attribute has a value of text.

Here is a basic example of a single-line text input used to take Here is a basic example of a single-line text input used to take first name and last namefirst name and last name

<form action="/cgi-bin/hello_get.cgi" method="get">First name: <input type="text" name="first_name" /><br>Last name: <input type="text" name="last_name" /><input type="submit" value="submit" /></form>

www.eshikshak.co.in

Page 7: Html forms

<Input> tag attributes<Input> tag attributes

Following is the list of attributes for <input> tag.Following is the list of attributes for <input> tag. type:type: Indicates the type of input control you want to create. Indicates the type of input control you want to create.

This element is also used to create other form controls such This element is also used to create other form controls such as radio buttons and checkboxes.as radio buttons and checkboxes.

name:name: Used to give the name part of the name/value pair Used to give the name part of the name/value pair that is sent to the server, representing each form control and that is sent to the server, representing each form control and the value the user entered.the value the user entered.

value:value: Provides an initial value for the text input control Provides an initial value for the text input control that the user will see when the form loads.that the user will see when the form loads.

size:size: Allows you to specify the width of the text-input Allows you to specify the width of the text-input control in terms of characters.control in terms of characters.

maxlength:maxlength: Allows you to specify the maximum number of Allows you to specify the maximum number of characters a user can enter into the text box.characters a user can enter into the text box.

www.eshikshak.co.in

Page 8: Html forms

Password input controlsPassword input controls

This is also a form of single-line text input controls are created This is also a form of single-line text input controls are created using an <input> element whose type attribute has a value of using an <input> element whose type attribute has a value of password.password.

Here is a basic example of a single-line password input used Here is a basic example of a single-line password input used to take user password:to take user password:

<form action="/cgi-bin/hello_get.cgi" method="get">Login : <input type="text" name="login" /><br>Password: <input type="text" name="password" /><input type="submit" value="submit" /></form>

www.eshikshak.co.in

Page 9: Html forms

Multiple-Line Text Input Multiple-Line Text Input ControlsControls To allow a visitor to your site to enter more than one line of text, you To allow a visitor to your site to enter more than one line of text, you

should create a multiple-line text input control using the <textarea> should create a multiple-line text input control using the <textarea> element.element.

Here is a basic example of a multi-line text input used to take item Here is a basic example of a multi-line text input used to take item description:description:

<form action="/cgi-bin/hello_get.cgi" method="get">Description : <br /><textarea rows="5" cols="50" name="description">Enter description here...</textarea><input type="submit" value="submit" /></form>

www.eshikshak.co.in

Page 10: Html forms

<textarea> tag attributes<textarea> tag attributes

name:name: The name of the control. This is used in the The name of the control. This is used in the name/value pair that is sent to the server.name/value pair that is sent to the server.

rows:rows: Indicates the number of rows of text area Indicates the number of rows of text area box.box.

cols:cols: Indicates the number of columns of text area Indicates the number of columns of text area box.box.

www.eshikshak.co.in

Page 11: Html forms

HTML Forms - Creating ButtonHTML Forms - Creating Button

There are various ways in HTML to create clickable There are various ways in HTML to create clickable buttons. You can create clickable button using <input> buttons. You can create clickable button using <input> tag.tag.

When you use the <input> element to create a button, When you use the <input> element to create a button, the type of button you create is specified using the type the type of button you create is specified using the type attribute. The type attribute can take the following attribute. The type attribute can take the following values:values: submit:submit: This creates a button that automatically submits a This creates a button that automatically submits a

form.form. reset:reset: This creates a button that automatically resets form This creates a button that automatically resets form

controls to their initial values.controls to their initial values. button:button: This creates a button that is used to trigger a client- This creates a button that is used to trigger a client-

side script when the user clicks that button.side script when the user clicks that button.

www.eshikshak.co.in

Page 12: Html forms

ExampleExample

<form action="http://www.example.com/test.asp" method="get"><input type="submit" name="Submit" value="Submit" /><br /><br /><input type="reset" value="Reset" /><input type="button" value="Button" /></form>

www.eshikshak.co.in

Page 13: Html forms

HTML Forms - Checkboxes HTML Forms - Checkboxes ControlControl Checkboxes are used when more than one option is required Checkboxes are used when more than one option is required

to be selected. They are created using <input> tag as shown to be selected. They are created using <input> tag as shown below.below.

Here is example HTML code for a form with two checkboxesHere is example HTML code for a form with two checkboxes

<form action="/cgi-bin/checkbox.cgi" method="get"><input type="checkbox" name="maths" value="on"> Maths<input type="checkbox" name="physics" value="on"> Physics<input type="submit" value="Select Subject" /></form>

www.eshikshak.co.in

Page 14: Html forms

Following is the list of important checkbox Following is the list of important checkbox attributes:attributes: type:type: Indicates that you want to create a checkbox. Indicates that you want to create a checkbox. name:name: Name of the control. Name of the control. value:value: The value that will be used if the checkbox is The value that will be used if the checkbox is

selected. More than one checkbox should share the selected. More than one checkbox should share the same name only if you want to allow users to select same name only if you want to allow users to select several items from the same list.several items from the same list.

checked:checked: Indicates that when the page loads, the Indicates that when the page loads, the checkbox should be selected.checkbox should be selected.

www.eshikshak.co.in

Page 15: Html forms

HTML Forms - Raidobox HTML Forms - Raidobox ControlControl Radio Buttons are used when only one option is required to be Radio Buttons are used when only one option is required to be

selected. They are created using <input> tag as shown below:selected. They are created using <input> tag as shown below: Here is example HTML code for a form with two radio Here is example HTML code for a form with two radio

button:button:

<form action="/cgi-bin/radiobutton.cgi" method="post"><input type="radio" name="subject" value="maths" /> Maths<input type="radio" name="subject" value="physics" /> Physics<input type="submit" value="Select Subject" /></form>

www.eshikshak.co.in

Page 16: Html forms

Following is the list of important radiobox Following is the list of important radiobox attributes:attributes: type:type: Indicates that you want to create a radiobox. Indicates that you want to create a radiobox. name:name: Name of the control. Name of the control. value:value: Used to indicate the value that will be sent to Used to indicate the value that will be sent to

the server if this option is selected.the server if this option is selected. checked:checked: Indicates that this option should be selected Indicates that this option should be selected

by default when the page loads.by default when the page loads.

www.eshikshak.co.in

Page 17: Html forms

HTML Forms - Select box HTML Forms - Select box ControlControl Drop Down Box is used when we have many options Drop Down Box is used when we have many options

available to be selected but only one or two will be selected..available to be selected but only one or two will be selected.. Here is example HTML code for a form with one drop down Here is example HTML code for a form with one drop down

boxbox<form action="/cgi-bin/dropdown.cgi" method="post"><select name="dropdown"><option value="Maths" selected>Maths</option><option value="Physics">Physics</option></select><input type="submit" value="Submit" /></form>

www.eshikshak.co.in

Page 18: Html forms

Following is the list of important attributes of <select>:Following is the list of important attributes of <select>: name:name: This is the name for the control. This is the name for the control. size:size: This can be used to present a scrolling list box. This can be used to present a scrolling list box. multiple:multiple: If set to "multiple" then allows a user to select If set to "multiple" then allows a user to select

multiple items from the menu.multiple items from the menu. Following is the list of important attributes of Following is the list of important attributes of

<option>:<option>: value:value: The value that is sent to the server if this option is The value that is sent to the server if this option is

selected.selected. selected:selected: Specifies that this option should be the initially Specifies that this option should be the initially

selected value when the page loads.selected value when the page loads. label:label: An alternative way of labeling options. An alternative way of labeling options.

www.eshikshak.co.in

Page 19: Html forms

HTML Forms - File Select BoxesHTML Forms - File Select Boxes

To allow a user to upload a file to your web site from his To allow a user to upload a file to your web site from his computer, you will need to use a file upload box, also known computer, you will need to use a file upload box, also known as a file select box. This is also created using the <input> as a file select box. This is also created using the <input> element.element.

Here is example HTML code for a form with one file select Here is example HTML code for a form with one file select boxbox<form action="/cgi-bin/hello_get.cgi" method="post"

name="fileupload" enctype="multipart/form-data"><input type="file" name="fileupload" accept="image/*" /></form>

www.eshikshak.co.in

Page 20: Html forms

HTML Forms - Hidden ControlsHTML Forms - Hidden Controls

To pass information between pages without the user To pass information between pages without the user seeing it. Hidden form controls remain part of any seeing it. Hidden form controls remain part of any form, but the user cannot see them in the Web browser. form, but the user cannot see them in the Web browser.

It should not be used for any sensitive information you It should not be used for any sensitive information you do not want the user to see because the user could see do not want the user to see because the user could see this data if she looked in the source of the page.this data if she looked in the source of the page.

Hidden form is being used to keep current page Hidden form is being used to keep current page number. number.

When a user will click next page then the value of When a user will click next page then the value of hidden form will be sent to the back-end application hidden form will be sent to the back-end application and it will decide which page has be displayed next.and it will decide which page has be displayed next.

www.eshikshak.co.in

Page 21: Html forms

HTML Forms - Hidden ControlsHTML Forms - Hidden Controls

<form action="/cgi-bin/hello_get.cgi" method="get" name="pages"><p>This is page 10</p><input type="hidden" name="pgaenumber" value="10" /><input type="submit" value="Next Page" /></form>

www.eshikshak.co.in

Page 22: Html forms

HTML Forms - Submit and Reset HTML Forms - Submit and Reset ButtonButton These are special buttons which can be created using <input> When These are special buttons which can be created using <input> When

submit button is clicked then Forms data is submitted to the back-end submit button is clicked then Forms data is submitted to the back-end application. When reset button is clicked then all the forms control are application. When reset button is clicked then all the forms control are reset to default state.reset to default state.

You already have seen submit button above, we will give one reset You already have seen submit button above, we will give one reset example here:example here:

<form action="/cgi-bin/hello_get.cgi" method="get">First name: <input type="text" name="first_name" /><br>Last name: <input type="text" name="last_name" /><input type="submit" value="Submit" /><input type="reset" value="Reset" /></form>

www.eshikshak.co.in