Techoceanhub logo

Html forms and input type

The <form> Element

The HTML <form> element is used to create an HTML form for user input:

<form>

<input type=”text” name=”txtFname”/>
</form>

The <form> element is a container for different types of input elements, such as: text fields, checkboxes, radio buttons, dropdown, submit buttons, etc.

Form Attributes:

The Action Attribute

The action attribute decides the action to be performed when the form is submitted.

Normally, the form data is sent to the server when the user clicks on the submit button.

Example

<form action=”/action_page.php”>

The Target Attribute

The target attribute specifies where to display the response on the same tab or different tag that is received after submitting the form.

The target attribute can have one of the following values:

The default value is _self which means that the response will open in the current browser window or Tab.

Example

Here, the submitted result will open in a new browser tab:

<form action=”/action_page.php” target=”_blank”>

ValueDescription
_blankThe response is displayed in a new window or tab
_selfThe response is displayed in the current window
_parentThe response is displayed in the parent frame
_topThe response is displayed in the full body of the window
framenameThe response is displayed in a named iframe

The Method Attribute

The method attribute specifies the HTTP method to be used when submitting the form data.

The form-data can be sent as URL variables with method="get" or as HTTP post uses method="post"

The default HTTP method when submitting form data is GET. 

Example

This example uses the GET method when submitting the form data:                           

<form action=”/action_page.php” method=”get”>

<form action=”/action_page.php” method=”post”>

Notes on GET:

  • It Appends the form data to the URL, in name/value pairs

http://localhost:8080/action_page.php?fname=test

  • Do not use GET  method to send sensitive data! (the submitted form data is visible in the URL!)
  • The length of a URL is limited (2048 characters)
  • GET is good for non-secure data

Notes on POST:

  • Appends the form data inside the body of the HTTP request (the submitted form data is not shown in the URL)
  • POST has no size limitations and can be used to send large amounts of data.
  • Form submissions with POST cannot be bookmarked.

The Novalidate Attribute

The novalidate attribute is a boolean attribute.

When we define novalidate, it specifies that the form-data (input) should not be validated when we submitted data.

<form action=”/action_page.php” novalidate>

The HTML <form> Elements

The HTML <form> element can contain one or more of the following form elements:

TagDescription
<form>Defines an HTML form for user input
<input>Defines an input control
<textarea>Defines a multiline input control (text area)
<label>Defines a label for an <input> element
<fieldset>Groups related elements in a form
<legend>Defines a caption for a <fieldset> element
<select>Defines a drop-down list
<optgroup>Defines a group of related options in a drop-down list
<option>Defines an option in a drop-down list
<button>Defines a clickable button
<datalist>Specifies a list of pre-defined options for input controls
<output>Defines the result of a calculation

HTML Input Types

Here are the different input types you can use in HTML:

  1. <input type=”text”>
  2. <input type=”button”>
  3. <input type=”submit”>
  4. <input type=”email”>
  5. <input type=”image”>
  6. <input type=”checkbox”>
  7. <input type=”color”>
  8. <input type=”date”>
  9. <input type=”datetime-local”>
  10. <input type=”file”>
  11. <input type=”hidden”>
  12. <input type=”month”>
  13. <input type=”number”>
  14. <input type=”password”>
  15. <input type=”radio”>
  16. <input type=”range”>
  17. <input type=”reset”>
  18. <input type=”search”>
  19. <input type=”tel”>
  20. <input type=”time”>
  21. <input type=”url”>
  22. <input type=”week”>

HTML Input Attributes

The value Attribute

The input value attribute specifies an initial value for an input field:

AttributeDescription
checkedIt specifies that an input field should be pre-selected when the page loads (for type=”checkbox” or type=”radio”)
disabledIt specifies that an input field should be disabled
maxIt specifies the maximum value for an input field
maxlengthIt specifies the maximum number of characters for an input field
minIt specifies the minimum value for an input field
patternIt specifies a regular expression to check the input value against
readonlyIt specifies that an input field is read only (cannot be changed)
requiredIt specifies that an input field is required (must be filled out)
sizeIt specifies the width (in characters) of an input field
stepIt specifies the legal number intervals for an input field
valueIt specifies the default value for an input field

Sample Example:

1)

Using Get method and input type text, password, submit and value and name attribute

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>    
        <form action="home.html" method="get">
            UserName:
            <input type="text" name="txtUsername" value="Type Username" /> <br>
            Password:
            <input type="password" name="txtPassword" /><br>
            <input type="submit"> 
        </form>
</body>
</html>

2)Using Post method and input type text, password, submit and value and name attribute

Recommended method for login form because of sensitive data username and password, so value never append in URL as a query parameter and it hides from user.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>    
        <form action="home.html" method="post">
            UserName:
            <input type="text" name="txtUsername" value="Type Username" /> <br>
            Password:
            <input type="password" name="txtPassword" /><br>
            <input type="submit"> 
        </form>
</body>
</html>

Output:

For Get method example:

After click on the submit you can notice browser URL something like below URL and your password clearly visible this is drawback of using get method for sensitive information.

http://127.0.0.1:5500/home.html?txtUsername=abc6663&txtPassword=password123

3) Simple Registration form:

This is simple registration form and most of the common input type used.  You can experiment with adding additional attribute and type in this form. Use form post method in registration form because you don’t even know what is size of data you are going to submit.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>    
        <form action="home.html" method="post">
            Registration form:
            <table cellpadding="5" cellspacing="5" border="1">
                <tr>
                    <td>Name</td>
                    <td> <input type="text" name="txtName" /></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td> <input type="email" name="txtemail" /></td>
                </tr>
                <tr>
                    <td>Phone</td>
                    <td> <input type="text" name="txtPhone" /></td>
                </tr>
                <tr>
                    <td>Count</td>
                    <td> <input type="number" name="txtNumber"  /></td>
                </tr>
                <tr>
                    <td>Gender</td>
                    <td> 
                        Male<input type="radio" name="radioGender"  value="Male"/>
                        Female<input type="radio" name="radioGender"  value="Female"/>
                    </td>
                </tr>            
                <tr>
                    <td>Hobbiles</td>
                    <td> 
                        <input type="checkbox" name="chkMusic" />Music
                        <input type="checkbox" name="chkSports" />Sports
                        <input type="checkbox" name="chkReading" />Reading
                    </td>
                </tr>
                <tr>
                    <td>Occupation</td>
                    <td> 
                        <select name="selOccupation">
                                <option value="">--Select--</option>
                                <option value="engineer">Engineer</option>
                                <option value="accountant">Accountant</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Upload File</td>
                    <td> 
                        <input type="file" name="fileupload" />
                    </td>
                </tr>
                <tr>
                    <td> <input type="submit"> <!-- or you can use button <input type="button" onclick="callFunction();"--> </td>
                    <td> 
                        <input type="reset" Value="Clear Data"> 
                    </td>
                </tr>                
            </table>                                               
        </form>
</body>
</html>

Output:

Feel free to read and comment below.

Below is my created application and is useful for people having Google Opinion Rewards.

https://play.google.com/store/apps/details?id=com.manasvi.sawant.rewardtocash

If you wanted to create a website, please visit my fiverr gig link below.

https://www.fiverr.com/share/EdZ9L7

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .