HTML: Form Tag

HTML forms are used to collect some data from the site visitor. The HTML <form>…..</form> tag is used to create an HTML form.

<input>
<textarea>
<button>
<select>
<option>
<optgroup>
<fieldset>
<label>
AttributeValueDescription
accept-charsetUTF-8Specifies the charset used in the submitted form (default: the page charset).
actionURLContains a URL that defines where to send the data after submitting the form
autocompleteOn (default)
Off
Determines that the browser retains the history of previous values.
enctype (default: is url-encoded)specifies how the browser encodes the data before it sends it to the server.
methodGET (default)
POST
Specifies how to send the form data to a web server. The data can be sent as URL variables, by using the get method or as HTTP post, by using the post method
namenameSpecifies a name used to identify the form
novalidatenovalidateSpecifies that the browser should not validate the form.
target_self (default)
_blank
_parent
_top
framename
Specifies the target of the address in the action attribute

Input Tag

<input> tag prompts the user to enter data and also request for the information from the web server after submitting the web form.

<form>
	<input>
</form>
No input end tag in HTML5
AttributeValueDescription
acceptfile_extension
audio/*
video/*
image/*
media_type
Specifies the types of files that the server accepts
alttextSpecifies an alternate text for an image. only used with type=“image”
autocompleteon
off
It enables or disable autocomplete in <input> tag
autofocusautofocusSpecifies that an <input> element should automatically get focus when the page loads
checkedcheckedInput element should be checked/selected when a web page loads. It is used only with  type=”checkbox” and type=”radio”
dirnameinputname.dirSpecifies that the text direction will be submitted
disableddisabledDisables the input element when it loads on the client side’s web page, so that user cannot write text in it or select it. This attribute cannot be used with type=“hidden”
formform_idSpecifies form’s id to which the input field belongs.
formactionURLSpecifies the URL of the file that will process the input control when the form is submitted
formenctypeapplication/x-www-form-urlencoded
multipart/form-data
text/plain
Specifies how the form-data should be encoded when submitting it to the server
formmethodGET
POST
Defines the HTTP method for sending data to the action URL
formnovalidateformnovalidateDefines that form elements should not be validated when submitted
formtarget_blank
_self
_parent
_top
framename
Specifies where to display the response that is received after submitting the form
heightpixelsIndicates the height of an input field
listId of a datalistContains the id of the <datalist> tag where a datalist element contains various options for an input field
maxnumber
date
Specifies the maximum value for an <input> element
number, range, date, datetime-local, month, time and week.
maxlength
minlength
numberSpecifies the maximum/minimum number of characters allowed in a text field.
minnumber
date
Specifies a minimum value for an <input> element
number, range, date, datetime-local, month, time and week.
multiplemultipleIndicates whether the user is allowed to enter more than one value
nametextDefines a unique name for the input element.
patternregexpSpecifies a regular expression that an <input> element’s value is checked against
placeholdertextSpecifies a short hint that describes the expected value
text, search, url, tel, email, and password
readonlyreadonlyIndicates that the value of this field cannot be modified.
requiredrequiredSpecifies that an input field must be filled out before submitting the form
sizenumberSpecifies the width of the control. If type=”text” or type=”password” this refers to the width in characters. Otherwise it’s in pixels.
srcURLDefines the URL of the image to display. It can be used only with type=“image”
stepnumberSpecifies the legal number intervals for an input field
typebutton
checkbox
color
date 
datetime 
datetime-local 
email 
file
hidden
image
month 
number 
password
radio
range 
reset
search
submit
tel
text
time 
url
week
Indicates the type of the input element. The default value is text.
valuetextSpecifies the value of an <input> element
widthpixelsIndicates the width of an input field

Type Attribute

It indicates the type of input element. It’s an empty tag. The default value is text.

Ex: –

<input> 
<input type=“text”>
  • Text – It defines a single-line text field. <input type=“text”>
  • Password – A password field is like text field, the difference being that this control hides each typed character by displaying an asterisk(*) or bullets(●) instead of the character itself. <input type=“password”>
  • Button – This is used to add a button on a web form to activate a script when an user click the button. <input type=“button”>
  • Email – This field is used to add an email address or a list of email address to a form, where type=“email” is a value for the input type. The input format should be an email like example@gmail.com else it will prompt an error. <input type=“email”>
  • Check Box – A check box is a small box, which when selected includes a checkmark. It is used to allow the user to select one or more than one of the options available on a web page. An User can select or clear the check box by clicking it. <input type=“checkbox”>
  • Radio Button – A radio button is used to create a series of options of which only one can be selected. It is displayed as a circle which when selected, displays a dot in the middle. <input type=“radio”>
  • •URL – The URL field is used to enter only the web addresses, in their correct format. If the URL is not entered in the correct format then the URL field validates the text field to enter web address. <input type=“url”> Autofocus – helps in keeping the focus of mouse pointer on the input field. Pattern – defines the regular expression of the text that should be entered in the text field.
  • Search Box – This is used to add a search box to a form. <input type=“search”>
  • Tel – The tel type represents a one-line plain-text edit for entering a telephone number. <input type=“tel”>
  • Range – Range input represents the input of limited range numerical values. <input type=“range”>
  • Number – Number is used to validate the textbox only if the value within the field is a numerical value. <input type=“number”>
  • File – This is used to upload a file on a web page. You also need to set the enctype=“multipart/form-data”. <input type=“file”>
  • Image – It represents either an image from which the UA enables a user to interactively select a pair of coordinates and submit the form, or alternatively a button from which the user can submit the form. <input type=“image”>
  • Hidden – A hidden control stores the data that is not visible to the user on a web page. This control is used to submit some information, which can not be edited by user. <input type=“hidden”>
  • Submit – A submit button is used to transfer form data to the URL specified in the <form action> tag.<input type=“submit”>
  • Reset Button – A reset button helps user to clear all the data that they have entered in the text fields.<input type=“reset”>
  • Date – This is used for input fields that should contain a date. <input type=“date”>
  • Time – It allows the user to select a time. <input type=“time”>
  • DateTime – It allows the user to select  date and time. <input type=“datetime”>
  • Datetime-local – It allows the user to select a date and time. <input type=“datetime-local”>
  • Month – It allows the user to select a month and year. <input type=“month”>
  • Week – It allows the user to select a week and year. <input type=“week”>
  • Color – It is used for input fields that should contain a color. <input type=“color”>

accept-charset Attribute

It specifies the character encodings that are to be used for the form submission.

ISO-8859-1 Character encoding for the Latin alphabet

UTF-8 Character encoding for Unicode

<form accept-charset=“UTF-8”>

Action Attribute

It contains a URL that defines where to send the data after submitting the form. It specifies the physical address of the server to which the user data should be redirected at the click of the submit button. action=http://www.something.com/something.html action=“page.html” action=“gotform.php” action=“example.asp”

If the action attribute is omitted, the action is set to the current page.

Autocomplete

On – The browser will automatically complete values based on values that the user has entered before. Off – User need to write all values each time they fill the form <form autocomplete=“on”>

enctype Attribute

It specifies how the browser encodes the data before it sends it to the server.

ValueDescription
application/x-www-form-urlencodedAll characters are encoded before sent (spaces are converted to “+” symbols, and special characters are converted to ASCII HEX values)
multipart/form-dataNo characters are encoded. This value is required when you are using forms that have a file upload control
text/plainSpaces are converted to “+” symbols, but no special characters are encoded
<form enctype=“multipart/form-data”>

Method Attribute

It Specifies how to send the form data to a web server. The data can be sent as URL variables, by using the get method or as HTTP post, by using the post method.

  • GET(default)
  • POST

GET

GET sends the data as part of the URL.

  • Appends form-data into the URL in name/value pairs.
  • The length of a URL is limited 2048 characters.
  • Never use GET method if you have password or other sensitive information to be sent to the server.
  • Useful for form submissions where a user want to bookmark the result.
  • GET is better for non-secure data, like query strings in Google.
  • GET can’t be used to send binary data, ex:-  images or word documents.
<form action=“some.php” method = “GET”>

If we use an invalid value for the method attribute of the <form> tag, the browser will use the default value GET.

POST

HTTP POST requests supply additional data from the client (browser) to the server in the message body.

  • Appends form-data inside the body of the HTTP request (data is not shown is in URL).
  • The POST method does not have any restriction on data size to be sent.
  • Form submissions with POST cannot be bookmarked.
  • The POST method can be used to send ASCII as well as binary data. Ex:-  image and word documents etc.
  • The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.
  • POST is a little safer than GET because the parameters are not stored in browser history or in web server logs.
<form action=“some.php” method = “POST”>
GETPOST
BACK button/ReloadHarmlessData will be re-submitted (the browser should alert the user that the data are about to be re-submitted)
BookmarkedCan be bookmarkedCannot be bookmarked
CachedCan be cachedNot cached
Encoding typeapplication/x-www-form-urlencodedapplication/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data
HistoryParameters remain in browser historyParameters are not saved in browser history
Restrictions on data typeOnly ASCII characters allowedNo restrictions. Binary data is also allowed
VisibilityData is visible to everyone in the URLData is not displayed in the URL

Name  Attribute

Specifies a name used to identify the form. It is Used to give a name to the control which is sent to the server to be recognized and get the value. name=“lastname”

novalidate

If we use novalidate attribute in form tag then the validation will not take place, even if there is a required filed.

<form novalidate>
<form novalidate=“novalidate”> 
<form novalidate=“true”> 

There is one more tag formnovalidate which is used by input tag to speficiy novlidate this particular text or field we will study about this later.

Text Direction (dirname)

Specifies that the text direction will be submitted

ValueDescription
ltrDefault. Left-to-right text direction
rtlRight-to-left text direction
autoLet the browser figure out the text direction, based on the content
<input type=“text” name=“city” dirname=“city.dir”>

Min and Max

number, range, date, datetime-local, month, time and week.

Tagged : / / / / / / / / / / / / / /

$_GET and $_POST Variable in PHP

  • $_GET and $_POST is generally used in the forms to transfer data from one file to another file. Both the variables are kind of same except when we need to keep our data secure we use $_POST and generally in the form we use $_POST.

We will see the explanation by a example:

Form.html

Testform.php

In this form we have used “get” method and we will see the output below:

As we can see we have used the method “get” in the form and in the output page of testform.php we can see the input given, also with that in the URL box we can see the details which we have provided in the form.html.

If we use “post” method in the form.html we will see the difference below:

As we can see in the URL box there is no such details provided which we can see while using “post” method.

Tagged : / / /