HTML - Basic forms

HTML Basic Forms

1. Introduction to HTML Forms

Forms in HTML allow users to enter data, such as text, numbers, or selections, and submit this information to a server. Forms are essential for interacting with users and collecting information. HTML forms are created using the <form> tag, and they consist of various input elements that enable users to provide data.

2. Basic Syntax of HTML Forms

The basic structure of an HTML form consists of the <form> tag and one or more input elements such as text fields, checkboxes, radio buttons, and submit buttons. The syntax for a form looks like this:

    <form action="submit_url" method="POST">
        <label>Label for input</label>
        <input type="text" name="input_name">
        <button type="submit">Submit</button>
    </form>
    

The action attribute specifies the URL where the form data will be sent, while the method attribute specifies how the data is sent (usually "GET" or "POST").

3. Form Elements

There are various form elements that allow users to input different types of data. Here are the most commonly used form elements:

3.1. Text Input

The <input> element with type="text" creates a single-line text input field. Users can enter text into this field.

    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    

3.2. Password Input

The <input> element with type="password" creates a password input field. This field hides the characters entered by the user.

    <label for="password">Password:</label>
    <input type="password" id="password" name="password">
    

3.3. Radio Buttons

Radio buttons are used when you want the user to select one option from a group. Use the <input> element with type="radio".

    <label>
        <input type="radio" name="gender" value="male"> Male
    </label>
    <label>
        <input type="radio" name="gender" value="female"> Female
    </label>
    

3.4. Checkboxes

Checkboxes allow users to select multiple options. The <input> element with type="checkbox" creates checkboxes.

    <label>
        <input type="checkbox" name="newsletter" value="subscribe"> Subscribe to newsletter
    </label>
    

3.5. Select Dropdown

The <select> element creates a dropdown menu. Inside the <select> tag, you can define <option> elements for each choice.

    <label for="country">Select a Country:</label>
    <select id="country" name="country">
        <option value="USA">United States</option>
        <option value="UK">United Kingdom</option>
        <option value="IN">India</option>
    </select>
    

3.6. Textarea

The <textarea> element creates a multi-line text input field, allowing users to input longer pieces of text.

    <label for="message">Message:</label>
    <textarea id="message" name="message"></textarea>
    

3.7. Submit Button

The <button> element with type="submit" creates a submit button that sends the form data to the server.

    <button type="submit">Submit</button>
    

4. Action and Method Attributes

The <form> tag has two important attributes: action and method:

4.1. Action Attribute

The action attribute specifies the URL to which the form data will be sent. It can point to a server-side script, such as a PHP or Node.js script.

    <form action="submit_form.php"> 

logo

HTML

Beginner 5 Hours

HTML Basic Forms

1. Introduction to HTML Forms

Forms in HTML allow users to enter data, such as text, numbers, or selections, and submit this information to a server. Forms are essential for interacting with users and collecting information. HTML forms are created using the <form> tag, and they consist of various input elements that enable users to provide data.

2. Basic Syntax of HTML Forms

The basic structure of an HTML form consists of the <form> tag and one or more input elements such as text fields, checkboxes, radio buttons, and submit buttons. The syntax for a form looks like this:

    <form action="submit_url" method="POST">
        <label>Label for input</label>
        <input type="text" name="input_name">
        <button type="submit">Submit</button>
    </form>
    

The action attribute specifies the URL where the form data will be sent, while the method attribute specifies how the data is sent (usually "GET" or "POST").

3. Form Elements

There are various form elements that allow users to input different types of data. Here are the most commonly used form elements:

3.1. Text Input

The <input> element with type="text" creates a single-line text input field. Users can enter text into this field.

    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    

3.2. Password Input

The <input> element with type="password" creates a password input field. This field hides the characters entered by the user.

    <label for="password">Password:</label>
    <input type="password" id="password" name="password">
    

3.3. Radio Buttons

Radio buttons are used when you want the user to select one option from a group. Use the <input> element with type="radio".

    <label>
        <input type="radio" name="gender" value="male"> Male
    </label>
    <label>
        <input type="radio" name="gender" value="female"> Female
    </label>
    

3.4. Checkboxes

Checkboxes allow users to select multiple options. The <input> element with type="checkbox" creates checkboxes.

    <label>
        <input type="checkbox" name="newsletter" value="subscribe"> Subscribe to newsletter
    </label>
    

3.5. Select Dropdown

The <select> element creates a dropdown menu. Inside the <select> tag, you can define <option> elements for each choice.

    <label for="country">Select a Country:</label>
    <select id="country" name="country">
        <option value="USA">United States</option>
        <option value="UK">United Kingdom</option>
        <option value="IN">India</option>
    </select>
    

3.6. Textarea

The <textarea> element creates a multi-line text input field, allowing users to input longer pieces of text.

    <label for="message">Message:</label>
    <textarea id="message" name="message"></textarea>
    

3.7. Submit Button

The <button> element with type="submit" creates a submit button that sends the form data to the server.

    <button type="submit">Submit</button>
    

4. Action and Method Attributes

The <form> tag has two important attributes: action and method:

4.1. Action Attribute

The action attribute specifies the URL to which the form data will be sent. It can point to a server-side script, such as a PHP or Node.js script.

    <form action="submit_form.php"> 

Frequently Asked Questions for HTML

  • HTML stands for HyperText Markup Language.
  • It is used to create the structure of web pages and web applications.
  • HTML defines elements such as headings, paragraphs, links, images, and other content.

  • Block-level elements (like <div>, <p>, <h1>) start on a new line and take full width.
  • Inline elements (like <span>, <a>, <strong>) stay within the flow of the text.
  • Understanding this helps with layout and styling.

  • A basic HTML page includes a <!DOCTYPE html> declaration, followed by <html>, <head>, and <body>.
  • The <head> section contains metadata like the title and links to stylesheets.
  • The <body> section contains all the visible content of the webpage.

  • The <meta> tag provides metadata such as page description, keywords, and author.
  • It helps browsers and search engines understand the content of the page.
  • One common use is specifying the character encoding: <meta charset="UTF-8">.

  • Forms collect user input using the <form> tag.
  • Inside a form, use <input>, <textarea>, <select>, and <button>.
  • The action attribute specifies where to send the form data.

  • The <label> tag defines a label for an input element.
  • It improves accessibility and allows users to click the label to focus the input.
    Example: <label for="email">Email:</label><input id="email">.

Comments in HTML are written between <!-- and -->.

Example:
<!-- This is a comment -->.
Comments are not displayed on the webpage and are used for documentation.

HTML entities are used to display reserved or special characters.

For example, &lt; displays < and &amp; displays &.
Use them to avoid confusion with actual HTML syntax.