← Back to HTML Course | Chapter 5: Forms | Lesson 1 of 7

HTML फॉर्म्स

कल्पना कीजिए कि आप पहली बार किसी डॉक्टर के क्लीनिक में जाते हैं। रिसेप्शनिस्ट आपको एक क्लिपबोर्ड थमाती है जिसमें एक खाली पेपर फॉर्म होता है जिसमें बॉक्स बने होते हैं जहाँ आपको अपना नाम, पता, और मेडिकल हिस्ट्री लिखनी होती है। आप उन बॉक्सों को भरकर क्लिपबोर्ड रिसेप्शनिस्ट को वापस कर देते हैं। HTML forms वही डिजिटल क्लिपबोर्ड हैं। ये आपकी वेबसाइट पर आने वाले विज़िटर्स को डेटा टाइप करने, ऑप्शन सिलेक्ट करने, और जानकारी सीधे आपके सर्वर पर सबमिट करने की सुविधा देते हैं। Forms सर्च queries, user feedback, अकाउंट्स, और इंटरैक्टिव कैलकुलेटर हैंडल करने के लिए बेहद ज़रूरी हैं।
Syntax
markup
<form action="url" method="post">
  <label for="id">Label</label>
  <input type="text" id="id" name="name">
  <button type="submit">Submit</button>
</form>

form एलिमेंट

form टैग वह कंटेनर है जो आपके सभी input fields, buttons, और selection menus को एक साथ ग्रुप करता है। यह ब्राउज़र को बताता है कि इस कंटेनर के अंदर दर्ज किया गया डेटा पैकेज करके सर्वर पर भेजा जाना चाहिए।

Note:
  • डेटा को सफलतापूर्वक सबमिट कराने के लिए हमेशा अपने input fields को एक form एलिमेंट के अंदर ग्रुप करें।
  • Accepted attributes:
  • action — URL that processes the form
  • method — get | post | dialog
  • enctype — application/x-www-form-urlencoded | multipart/form-data | text/plain
  • target — _self | _blank | _parent | _top
  • autocomplete — on | off
  • novalidate — boolean
Warning: input fields को form कंटेनर के बाहर रखने से डेटा सही तरीके से पैकेज और सबमिट नहीं हो पाता।

उदाहरण: The form Element

markup
<form action="/submit" method="post">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username">
</form>

Text Inputs और Labels

input टैग एक ऐसा इनपुट फील्ड डिफाइन करता है जहाँ यूज़र डेटा दर्ज कर सकते हैं, और label टैग उस फील्ड के लिए एक स्पष्ट टेक्स्ट डिस्क्रिप्शन देता है। अपने labels को inputs से लिंक करने से आपके फॉर्म काफी ज़्यादा accessible और क्लिक करने में आसान बन जाते हैं।

Note:
  • अपने label के for attribute को input के id attribute से मैच कराकर उन्हें आपस में लिंक करें।
  • Accepted attributes:
  • input type — text | password | email | number | search | tel | url
  • name — key used when submitting
  • id — must match the label for attribute
  • label for — id of the associated control
  • placeholder / required / value
Warning: अपने labels को inputs से लिंक न करने पर screen-reader इस्तेमाल करने वाले यूज़र्स के लिए आपके forms नेविगेट करना मुश्किल हो जाता है।

उदाहरण: Text Inputs and Labels

markup
<form action="/submit" method="post">
  <label for="email">Email:</label>
  <input type="text" id="email" name="email">
</form>

Selection Buttons (Radio और Checkboxes)

Radio buttons का उपयोग तब किया जाता है जब आप चाहते हैं कि यूज़र किसी लिस्ट में से ठीक एक ऑप्शन चुनें, जबकि checkboxes यूज़र को एक साथ कई ऑप्शन चुनने की सुविधा देते हैं।

Note:
  • radio buttons को एक साथ ग्रुप करने के लिए सुनिश्चित करें कि उन सभी का name attribute बिल्कुल एक जैसा हो।
  • Accepted attributes:
  • type — radio | checkbox
  • name — radios with the same name form one group
  • value — data sent when selected
  • checked — boolean; preselects
  • required / disabled — booleans
Warning: अगर आप अपने radio buttons को एक जैसा name देना भूल जाते हैं, तो यूज़र्स एक साथ कई ऑप्शन सिलेक्ट कर पाएंगे, जिससे इसका मकसद ही खत्म हो जाता है।

उदाहरण: Selection Buttons (Radio and Checkboxes)

markup
<form action="/submit" method="post">
  <input type="radio" id="plan-basic" name="plan" value="basic">
  <label for="plan-basic">Basic</label>

  <input type="radio" id="plan-pro" name="plan" value="pro">
  <label for="plan-pro">Pro</label>

  <input type="checkbox" id="newsletter" name="newsletter">
  <label for="newsletter">Subscribe</label>
</form>

Dropdown Selection Menus (select)

select एलिमेंट का उपयोग एक साफ-सुथरा, कॉम्पैक्ट dropdown मेनू बनाने के लिए किया जाता है। यह ऑप्शन की लिस्ट (जैसे देश या file types) दिखाने के लिए एकदम सही है, बिना आपके पेज लेआउट को गन्दा किए।

Note:
  • अपनी लिस्ट के सबसे ऊपर एक disabled selected ऑप्शन जोड़ें, जो एक placeholder instruction की तरह काम करे।
  • Accepted attributes:
  • select name — key submitted
  • select multiple — boolean; allow more than one selection
  • select size — number of visible rows
  • option value — data sent
  • option selected — boolean; preselected
  • option disabled — boolean
Warning: सुनिश्चित करें कि आप हर option टैग पर value attribute शामिल करें, क्योंकि यही डेटा आपके सर्वर पर भेजा जाता है।

उदाहरण: Dropdown Selection Menus (select)

markup
<form action="/submit" method="post">
  <label for="country">Country:</label>
  <select id="country" name="country">
    <option value="us">United States</option>
    <option value="in">India</option>
  </select>
</form>

Submit Action Button

एक HTML फॉर्म डेटा सबमिशन को ट्रिगर करने वाले बटन के बिना अधूरा है। अपने बटन के type attribute को submit सेट करने पर ब्राउज़र को निर्देश मिलता है कि वह सारे फॉर्म डेटा को पैकेज करके सीधे आपके सर्वर पर भेज दे।

Note:
  • एक स्पष्ट button label (जैसे Submit या Upload) इस्तेमाल करें ताकि यूज़र को पता चले कि बटन क्लिक करने से क्या होगा।
  • Accepted attributes:
  • type — submit
  • value — button label (on input type=submit)
  • formaction / formmethod / formenctype / formtarget / formnovalidate — per-button overrides
Warning: अगर आप type attribute स्पष्ट नहीं करते, तो कुछ ब्राउज़र्स डिफ़ॉल्ट रूप से बटन को submit बटन मान लेते हैं, जिससे अनचाहे submissions हो सकते हैं।

उदाहरण: The Submit Action Button

markup
<form action="/submit" method="post">
  <button type="submit">Submit</button>
</form>

Form Validation Attributes

HTML5 अपने साथ built-in form validation लेकर आया। आप बिना कोई JavaScript लिखे फील्ड्स को required बना सकते हैं, minimum और maximum length सेट कर सकते हैं, और यहाँ तक कि patterns के खिलाफ भी वैलिडेट कर सकते हैं।

Note:
  • product codes जैसे कस्टम फॉर्मेट को वैलिडेट करने के लिए regex के साथ pattern attribute का उपयोग करें।
  • Accepted attributes:
  • required — boolean
  • minlength / maxlength — integer text length
  • min / max — number, date or time bounds
  • step — number | any
  • pattern — regular expression
Warning:
  • कभी भी सिर्फ browser validation पर निर्भर न रहें।
  • अपने डेटा को हमेशा अपने सर्वर पर भी validate करें।

उदाहरण: Form Validation Attributes

markup
<form action="/submit" method="post">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" required minlength="3" maxlength="10" pattern="[A-Za-z]+">
</form>

Fieldset और Legend

fieldset एलिमेंट संबंधित form fields को एक साथ ग्रुप करता है। legend एलिमेंट उस ग्रुप को एक दिखने वाला टाइटल देता है। यह accessibility और लंबे फॉर्म्स के लिए खासतौर पर महत्वपूर्ण है।

Note:
  • shipping address को billing address से अलग जैसे संबंधित फील्ड्स को ग्रुप करने के लिए fieldset का उपयोग करें।
  • Accepted attributes:
  • fieldset disabled — boolean; disables all contained controls
  • fieldset form — id of an associated form
  • fieldset name — name of the group
  • legend — first child; only global attributes
Warning: जटिल forms में fieldset को छोड़ देने से वे screen reader यूज़र्स के लिए confusing बन जाते हैं।

उदाहरण: Fieldset and Legend

markup
<form action="/submit" method="post">
  <fieldset>
    <legend>Shipping Address</legend>
    <label for="street">Street:</label>
    <input type="text" id="street" name="street">
  </fieldset>
</form>

enctype Attribute

enctype attribute ब्राउज़र को बताता है कि भेजने से पहले फॉर्म डेटा को कैसे पैकेज किया जाए। file uploads के लिए आपको multipart/form-data चाहिए होता है, वरना ब्राउज़र असली फ़ाइल की जगह सिर्फ उसका नाम plain text के रूप में भेज देता है।

Note:
  • जिस भी फॉर्म में file input हो, उसमें हमेशा enctype को multipart/form-data सेट करें।
  • Accepted values:
  • application/x-www-form-urlencoded — default
  • multipart/form-data — required for file uploads
  • text/plain — plain text, mainly debugging
Warning: file upload वाले form में enctype भूल जाना सबसे common bugs में से एक है — file field चुपचाप fail हो जाता है और कुछ भी उपयोगी अपलोड नहीं होता।

उदाहरण: The enctype Attribute

markup
<form action="/upload" method="post" enctype="multipart/form-data">
  <label for="document">Document:</label>
  <input type="file" id="document" name="document">
</form>

autocomplete Attribute

autocomplete attribute यह नियंत्रित करता है कि ब्राउज़र किसी फील्ड के लिए पहले दर्ज की गई वैल्यू सुझा सकता है या नहीं। इसे पूरे फॉर्म पर या individual inputs पर सेट किया जा सकता है, और ब्राउज़र इसका उपयोग लौटने वाले विज़िटर्स के लिए बार-बार टाइपिंग को तेज़ करने में करते हैं।

Note:
  • autocomplete="email" या autocomplete="tel" जैसे specific values इस्तेमाल करें, ताकि ब्राउज़र सिर्फ raw history से नहीं बल्कि समझदारी से फील्ड्स भर सके।
  • Accepted values:
  • on — allow browser autofill
  • off — disable autofill
  • name | email | username | new-password | current-password | tel | street-address | postal-code | cc-number — specific autofill tokens
Warning: passwords जैसे sensitive fields पर autocomplete बंद करने से प्राइवेसी की गारंटी नहीं मिलती — कुछ ब्राउज़र्स इस सेटिंग को पूरी तरह इग्नोर कर देते हैं।

उदाहरण: The autocomplete Attribute

markup
<form action="/submit" method="post">
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" autocomplete="email">

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

novalidate Attribute

डिफ़ॉल्ट रूप से, अगर required या pattern जैसे built-in validation rules पूरे नहीं होते तो ब्राउज़र फॉर्म सबमिशन को रोक देते हैं।

form टैग में novalidate जोड़ने से वह ऑटोमैटिक चेकिंग बंद हो जाती है, जो तब उपयोगी होती है जब आप खुद JavaScript में validation संभालना चाहते हैं।

Note:
  • जब डिफ़ॉल्ट ब्राउज़र वैलिडेशन पॉपअप आपकी साइट डिज़ाइन से मेल नहीं खाते, तो अपने custom error messages के साथ novalidate का उपयोग करें।
  • Accepted values:
  • novalidate — boolean attribute on form
  • Disables built-in constraint validation on submit
  • Per-button alternative: formnovalidate
Warning: अगर आप novalidate इस्तेमाल करते हैं, तो डेटा validate करने की पूरी ज़िम्मेदारी आपकी है — ब्राउज़र गलत इनपुट को सबमिट होने से नहीं रोकेगा।

उदाहरण: The novalidate Attribute

markup
<form action="/submit" method="post" novalidate>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required>
</form>

datalist एलिमेंट

datalist किसी input के लिए सुझाई गई वैल्यू की एक लिस्ट देता है, बिना यूज़र को सिर्फ उसी लिस्ट में से चुनने के लिए मजबूर किए — select dropdown के उलट। यूज़र फिर भी अपनी वैल्यू टाइप कर सकते हैं, लेकिन टाइप करते समय उन्हें सुझाव भी दिखते हैं।

Note:
  • datalist उन चीज़ों के लिए बढ़िया है जैसे country names या product categories, जहाँ आप बहुत ज़्यादा प्रतिबंधित किए बिना इनपुट को गाइड करना चाहते हैं।
  • Accepted attributes:
  • datalist id — referenced by the input list attribute
  • input list — id of the datalist
  • option value — suggested value
  • option label — optional display text
Warning: datalist की styling को ज़्यादा customize नहीं किया जा सकता — यह ब्राउज़र के native suggestion popup का इस्तेमाल करता है, जो हर ब्राउज़र में अलग दिखता है।

उदाहरण: The datalist Element

markup
<form action="/submit" method="post">
  <label for="country">Country:</label>
  <input list="countries" id="country" name="country">
  <datalist id="countries">
    <option value="India">
    <option value="United States">
  </datalist>
</form>

GET बनाम POST की समझ

GET और POST किसी फॉर्म द्वारा डेटा भेजने के दो सबसे सामान्य तरीके हैं। GET डेटा को URL में एक query string के रूप में जोड़ देता है, जिससे वह दिखने योग्य और bookmark करने योग्य बन जाता है, जो searches के लिए एकदम सही है।

POST डेटा को request body में अदृश्य रूप से भेजता है, जिससे यह passwords जैसी संवेदनशील चीज़ों या file uploads जैसी बड़ी चीज़ों के लिए बेहतर होता है।

Note:
  • search forms और filters के लिए GET का उपयोग करें जहाँ URL शेयर करने पर वही नतीजे दोबारा मिलने चाहिए, और signups या purchases जैसी डेटा बदलने वाली चीज़ों के लिए POST का उपयोग करें।
  • Accepted values:
  • method — get | post | dialog
  • get — data appended to URL query string
  • post — data sent in the request body
Warning: passwords या personal data वाले forms के लिए कभी भी GET इस्तेमाल न करें — values ब्राउज़र history और server logs में दिखाई देने लगती हैं।

उदाहरण: GET vs POST Explained

markup
<form action="/search" method="get">
  <label for="q">Search:</label>
  <input type="text" id="q" name="q">
</form>

<form action="/signup" method="post">
  <label for="password">Password:</label>
  <input type="password" id="password" name="password">
</form>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.