Lecture 5:Forms in HTML

Principle

  1. Basic Form Structure
  2. <form> <label for="firstName">First Name:</label> <input type="text" id="firstName" name="firstName"> <br><br> <button type="submit">Submit</button> </form>

  3. Core Elements
  4. <input> type="text" → single line input. type="password" → hidden characters. type="number" → numeric input with min/max. type="date" → calendar picker. type="file" → upload file. type="color", type="range", etc. → specialized inputs. <label> Describes input field. Use for="id" → links label with input (click label = focus input). Attributes: id → unique per page (used with label). name → key sent to server (critical for data submission). placeholder → hint text inside input. required → makes field mandatory.

  5. Choice Inputs
  6. Radio Buttons (Select One)

            <input type="radio" id="sizeM" name="shirtSize" value="medium">
            <label for="sizeM">Medium</label>
                

    Checkboxes (Select Many)

                <input type="checkbox" id="mush" name="toppings" value="mushrooms">
                <label for="mush">Mushrooms</label>
                

    <!-- Dropdown -->

                <select id="country" name="country">
               <option value="">--Choose--</option>
               <option value="in">India</option>
               <option value="us">USA</option>
                </select>
    
    
    
                

  7. Longer Text
  8. <textarea rows="4" cols="50"></textarea> → multi-line input.

  9. Buttons
  10. <input type="submit"> → basic submit. <button type="submit"> → flexible, can hold text, <strong>, <img>. </button>