Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Thursday, October 2, 2014

HTML FORM Quick Tutorial

.
HTML FORM Quick Tutorial
This is a simplified version of the original article.

1.HTML <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.

2.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)

3.Basic Form Codes

Source code:
<html>
<body>
<form action="https://codepen.io/search/pens">
  search keywords:<br>
  <input type="text" name="q" value="Mickey">
  <br><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>
Codepen:
See the Pen HTMLFORM101-Basic Form by NotaRaziCom (@notarazicom) on CodePen.

4. 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 2010s trend in web development is Single Page Application. Single-Page Applications (SPAs) are Web apps that load a single HTML page and dynamically update that page as the user interacts with the app.
To prevent the page to reload itself or call another page when submit event triggers, we need give instructions to cancel the event.

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur.

For example, this can be useful when:

Clicking on a "Submit" button, prevent it from submitting a form
Clicking on a link, prevent the link from following the URL

Note: Not all events are cancelable. Use the cancelable property to find out if an event is cancelable.

Note: The preventDefault() method does not prevent further propagation of an event through the DOM. Use the stopPropagation() method to handle this.

Further readings:

5. 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.
If the action attribute is omitted, the action is set to the current page.

6. The Method Attribute

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


6a. 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
Note: GET must NOT be used when sending sensitive information! GET is best suited for short, non-sensitive, amounts of data, because it has size limitations too.

6b. 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.

POST has no size limitations, and can be used to send large amounts of data.
Read more on sending process here (mozilla developer guide).

7. 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.

8. Form Object

The Form object represents an HTML <form> element.

You can access a <form> element by using getElementById():
The submit() method submits the form (same as clicking the Submit button).
document.getElementById("myForm").submit();
The reset() method resets the values of all elements in a form (same as clicking the Reset button).
document.getElementById("myForm").reset();
Further readings:

Further Readings:

HTML tutorial: HTML Forms

JavaScript tutorial: JS Forms/Validation

HTML reference: HTML <form> tag

.
201410

No comments:

Post a Comment