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

HTML Input Form Attributes

कल्पना कीजिए कि आप एक पेपर जॉब एप्लीकेशन भर रहे हैं। मुख्य फॉर्म आपकी basic details को पैकेज करके HR ऑफिस भेजता है। लेकिन, पेज के नीचे एक स्पेशल बटन है जो आपके फाइल रिज्यूमे को पैकेज करके इसके बजाय सीधे इंजीनियरिंग टीम को भेजता है। HTML input form attributes वही स्पेशल कंट्रोल्स हैं। ये आपके पेज पर दिखने वाले input fields को नहीं बदलते, बल्कि ये specific बटनों या fields को parent form टैग की सेटिंग्स को override करने देते हैं। आप इन attributes का इस्तेमाल करके अलग-अलग बटनों को अलग-अलग server endpoints पर डेटा सबमिट कराने, request methods बदलने, या जरूरत पड़ने पर validation checks bypass करने के लिए कर सकते हैं।
Syntax
markup
<input type="submit" form="formid" formaction="url" formmethod="post" formtarget="_blank">

form Attribute

डिफ़ॉल्ट रूप से, input फील्ड्स को अपना डेटा सबमिट करने के लिए एक form कंटेनर के अंदर रखा जाना चाहिए। form attribute आपको input फील्ड्स को अपने पेज पर कहीं भी (यहाँ तक कि form टैग के बाहर भी) रखने देता है और उन्हें उसकी unique ID का उपयोग करके किसी specific फॉर्म से लिंक करने देता है।

Note:
  • form टैग की nesting से बंधे बिना जटिल, रचनात्मक लेआउट बनाने के लिए form attribute का उपयोग करें।
  • Accepted values:
  • form — the id of a form element in the same document
  • Lets a control outside the form belong to it
  • Valid on input, button, select, textarea, fieldset, output, object
Warning: सुनिश्चित करें कि form attribute की value बिल्कुल target form की ID से मैच करे, वरना डेटा सबमिट नहीं होगा।

उदाहरण: The form Attribute

markup
<form id="myForm" action="/submit"></form>
<input type="text" name="username" form="myForm">

formaction से Endpoints Override करना

formaction attribute submit बटन पर उपयोग किया जाता है ताकि पैरेंट form टैग के डिफ़ॉल्ट action URL को ओवरराइड किया जा सके। इससे आप अलग-अलग बटन बना सकते हैं जो बिल्कुल एक जैसा फॉर्म डेटा अलग-अलग server endpoints पर भेजते हैं।

Note:
  • एक ही फॉर्म पर अलग 'Save Draft' और 'Publish Live' बटन बनाने के लिए formaction attribute का उपयोग करें।
  • Accepted values:
  • formaction — any valid URL
  • Valid on input type=submit|image and button
  • Overrides the form action
Warning: सुनिश्चित करें कि आपके formaction URLs पूरी तरह सही और सुरक्षित हों, वरना आपके form submissions फेल हो जाएंगे।

उदाहरण: Overriding Endpoints with formaction

markup
<form action="/save">
  <button type="submit" formaction="/save-draft">Save Draft</button>
  <button type="submit" formaction="/publish">Publish</button>
</form>

formenctype से Data Formats बदलना

formenctype attribute submit बटन पर उपयोग किया जाता है ताकि पैरेंट form टैग के डिफ़ॉल्ट encoding type को ओवरराइड किया जा सके।

यह तब बेहद उपयोगी होता है जब आप चाहते हैं कि एक specific बटन फाइलें अपलोड करे (जिसके लिए multipart/form-data चाहिए होता है) जबकि बाकी फील्ड्स स्टैण्डर्ड टेक्स्ट ही बने रहें।

Note:
  • एक ही फॉर्म पर text फील्ड्स और file uploads दोनों को डायनामिक रूप से संभालने के लिए formenctype attribute का उपयोग करें।
  • Accepted values:
  • application/x-www-form-urlencoded
  • multipart/form-data
  • text/plain
Warning: file uploads पर multipart/form-data encoding type स्पष्ट न करने से आपकी file transfers फेल हो जाएंगी।

उदाहरण: Changing Data Formats with formenctype

markup
<form action="/submit">
  <input type="file" name="file">
  <button type="submit" formenctype="multipart/form-data">Upload</button>
</form>

formmethod से Request Methods बदलना

formmethod attribute submit बटन पर उपयोग किया जाता है ताकि पैरेंट form टैग के डिफ़ॉल्ट HTTP request method (GET या POST) को ओवरराइड किया जा सके, जिससे आप अपना डेटा किस तरह ट्रांसमिट होता है इसे तुरंत बदल सकते हैं।

Note:
  • public GET requests और secure POST requests के बीच जल्दी टॉगल करने के लिए formmethod का उपयोग करें।
  • Accepted values:
  • get
  • post
  • dialog
Warning: sensitive user data भेजने के लिए कभी भी GET method इस्तेमाल न करें, क्योंकि वे ब्राउज़र के address bar में खुलेआम दिखाई देंगे।

उदाहरण: Changing Request Methods with formmethod

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

formnovalidate से Form Validation को बायपास करना

डिफ़ॉल्ट रूप से, ब्राउज़र सबमिट करने से पहले फॉर्म फील्ड्स पर बेसिक validation checks करते हैं।

formnovalidate attribute submit बटन पर उपयोग किया जाता है ताकि इन native चेक्स को बायपास किया जा सके, जिससे आप required फील्ड्स खाली होने पर भी drafts सेव कर सकते हैं या एक्शन कैंसल कर सकते हैं।

Note:
  • 'Save Draft' बटन पर formnovalidate attribute का उपयोग करें ताकि यूज़र सभी required फील्ड्स भरे बिना भी अपनी प्रगति सेव कर सकें।
  • Accepted values:
  • formnovalidate — boolean attribute
  • Valid on input type=submit|image and button
  • Skips validation only for that button
Warning: अगर आप validation bypass करते हैं, तो खाली forms सबमिट होने से रोकने के लिए ज़रूरी checks आपको अपने सर्वर पर हैंडल करने होंगे।

उदाहरण: Bypassing Form Validation with formnovalidate

markup
<form action="/submit">
  <input type="email" required>
  <button type="submit" formnovalidate>Save Draft</button>
</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.