(36) HTML (Form)

¡Supera tus tareas y exámenes ahora con Quizwiz!

HTML Form Example

<!DOCTYPE html> <html> <body> <h2>HTML Forms</h2> <form action="/action_page.php"> First name:<br> <input type="text" name="firstname" value="Mickey"> <br> Last name:<br> <input type="text" name="lastname" value="Mouse"> <br><br> <input type="submit" value="Submit">

Radio Button Input

<input type="radio"> defines a radio button. Radio buttons let a user select ONE of a limited number of choices: Example <form> <input type="radio" name="gender" value="male" checked> Male<br> <input type="radio" name="gender" value="female"> Female<br> <input type="radio" name="gender" value="other"> Other </form>

The Submit Button

<input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a server page with a script for processing input data. The form-handler is specified in the form's action attribute: Example <form action="/action_page.php"> First name:<br> <input type="text" name="firstname" value="Mickey"><br> Last name:<br> <input type="text" name="lastname" value="Mouse"><br><br> <input type="submit" value="Submit"> </form>

Text Input

<input type="text"> defines a one-line input field for text input: Example <form> First name:<br> <input type="text" name="firstname"><br> Last name:<br> <input type="text" name="lastname"> </form> Note: The form itself is not visible. Also note that the default width of a text field is 20 characters.

When to Use POST

Always use POST if the form data contains sensitive or personal information. The POST method does not display the submitted form data in the page address field. Notes on POST: POST has no size limitations, and can be used to send large amounts of data. Form submissions with POST cannot be bookmarked

HTML Forms

An HTML form is used to collect user input. The user input is most often sent to a server for processing. <!DOCTYPE html> <html> <body> <h2>HTML Forms</h2> <form action="/action_page.php"> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname" value="John"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname" value="Doe"><br><br> <input type="submit" value="Submit"> </form> <p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p> </body> </html>

<form> attribute

Attribute Description accept-charset - Specifies the charset used in the submitted form (default: the page charset). action - Specifies an address (url) where to submit the form (default: the submitting page). autocomplete - Specifies if the browser should autocomplete the form (default: on). enctype - Specifies the encoding of the submitted data (default: is url-encoded). method - Specifies the HTTP method used when submitting the form (default: GET). name - Specifies a name used to identify the form (for DOM usage: document.forms.name). novalidate - Specifies that the browser should not validate the form. target - Specifies the target of the address in the action attribute (default: _self).

The Name Attribute

Each input field must have a name attribute to be submitted. If the name attribute is omitted, the data of that input field will not be sent at all. This example will only submit the "Last name" input field: Example <form action="/action_page.php"> First name:<br> <input type="text" value="Mickey"><br> Last name:<br> <input type="text" name="lastname" value="Mouse"><br><br> <input

The <label> Element

Notice the use of the <label> element in the example above. The <label> tag defines a label for many form elements. The <label> element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focus on the input element. The <label> element also help users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) - because when the user clicks the text within the <label> element, it toggles the radio button/checkbox. The for attribute of the <label> tag should be equal to the id attribute of the <input> element to bind them together.

Grouping Form Data with <fieldset>

The <fieldset> element is used to group related data in a form. The <legend> element defines a caption for the <fieldset> element. Example <form action="/action_page.php"> <fieldset> <legend>Personal information:</legend> First name:<br> <input type="text" name="firstname" value="Mickey"><br> Last name:<br> <input type="text" name="lastname" value="Mouse"><br><br> <input type="submit" value="Submit"> </fieldset> </form>

Checkboxes

The <input type="checkbox"> defines a checkbox. Checkboxes let a user select ZERO or MORE options of a limited number of choices. <!DOCTYPE html> <html> <body> <h2>Checkboxes</h2> <p>The <strong>input type="checkbox"</strong> defines a checkbox:</p> <form action="/action_page.php"> <input type="checkbox" id="vehicle1" name="vehicle1" value="Bike"> <label for="vehicle1"> I have a bike</label><br> <input type="checkbox" id="vehicle2" name="vehicle2" value="Car"> <label for="vehicle2"> I have a car</label><br> <input type="checkbox" id="vehicle3" name="vehicle3" value="Boat"> <label for="vehicle3"> I have a boat</label><br><br> <input type="submit" value="Submit"> </form> </body> </html>

Text Fields

The <input type="text"> defines a single-line input field for text input. <!DOCTYPE html> <html> <body> <h2>Text input fields</h2> <form> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname" value="John"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname" value="Doe"> </form> <p>Note that the form itself is not visible.</p> <p>Also note that the default width of text input fields is 20 characters.</p> </body> </html> Text input fields First name: Last name: Note that the form itself is not visible. Also note that the default width of text input fields is 20 characters. Note: The form itself is not visible. Also note that the default width of an input field is 20 characters.

The <input> Element

The <input> element is the most important form element. The <input> element can be displayed in several ways, depending on the type attribute. Here are some examples: Type Description <input type="text"> Defines a one-line text input field <input type="radio"> Defines a radio button (for selecting one of many choices) <input type="submit"> Defines a submit button (for submitting the form)

The <form> Element

The HTML <form> element defines a form that is used to collect user input: <form> . form elements . </form> An HTML form contains form elements. Form elements are different types of input elements, like text fields, checkboxes, radio buttons, submit buttons, and more.

The Action Attribute

The action attribute defines the action to be performed when the form is submitted. Normally, the form data is sent to a web page on the server when the user clicks on the submit button. In the example above, the form data is sent to a page on the server called "/action_page.php". This page contains a server-side script that handles the form data: <form action="/action_page.php"> If the action attribute is omitted, the action is set to the current page.

When to Use GET

The default method when submitting form data is GET. However, when GET is used, the submitted form data will be visible in the page address field: /action_page.php?firstname=Mickey&lastname=Mouse Notes on GET: Appends form-data into the URL in name/value pairs The length of a URL is limited (2048 characters) Never use GET to send sensitive data! (will be visible in the URL) Useful for form submissions where a user wants to bookmark the result GET is better for non-secure data, like query strings in Google

The Method Attribute

The method attribute specifies the HTTP method (GET or POST) to be used when submitting the form data: Example <form action="/action_page.php" method="get"> or: Example <form action="/action_page.php" method="post">

The Target Attribute

The target attribute specifies if the submitted result will open in a new browser tab, a frame, or in the current window. The default value is "_self" which means the form will be submitted in the current window. To make the form result open in a new browser tab, use the value "_blank": Example <form action="/action_page.php" target="_blank"> Other legal values are "_parent", "_top", or a name representing the name of an iframe.


Conjuntos de estudio relacionados

Chapter 11 (Membranes)- MCAT Prep Questions

View Set

Physics: resistors in series and parallel

View Set

Med-Surg II: Final Exam: Burns and Emergency Nursing

View Set

Ch. 23 administrative agencies + quiz questions

View Set