H

HTML Handbook

Clean • Professional

Form Attributes

3 minute

Form Attributes in HTML

Form attributes are special properties added to the <form> tag or its input elements to control how the form behaves and how data is sent to the server.

action Attribute

  • The action attribute in a <form> specifies the URL or file where the form data will be sent after the user submits it.

  • If action is not provided, the form submits data to the same page.

Example:

<form action="submit.php" method="post">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required>
  <input type="submit" value="Submit">
</form>

Output : 

learn code with durgesh images

method Attribute in Forms

The method attribute in HTML forms defines how the form data will be sent to the server when the user clicks the submit button.

There are two main methods:

GET Method

Sends the form data as part of the URL (in the query string).

Example:

<form action="search.php" method="get">
  <input type="text" name="query" placeholder="Search...">
  <input type="submit" value="Go">
</form>

Output :

learn code with durgesh images

POST Method

Sends form data in the HTTP request body, not visible in the URL.

More secure than GET, especially for sensitive data (passwords, login forms).

Example:

<form action="submit.php" method="post">
  <label>Email:</label>
  <input type="email" name="email" required>
  <input type="submit" value="Submit">
</form>

Output :

learn code with durgesh images

Useful Form Attributes

 

AttributePurpose
actionDefines where the form data will be sent (URL or file).
methodDefines how data is sent (GET = URL, POST = body).
enctypeSpecifies encoding type for form data (needed for file upload).
targetDefines where to display response (_self, _blank, etc.).
autocompleteEnables/disables autofill suggestions (on / off).
novalidateSubmits form without HTML5 validation.
accept-charsetDefines character encoding (e.g., UTF-8).
nameIdentifies the input field (used in backend/JS).
valueSets default value for an input.
placeholderShows a hint inside the input box.
requiredMakes a field mandatory before submission.
patternValidates input using a regular expression.
min / maxSets numeric/date input limits.
minlength / maxlengthRestricts number of characters in text input.
readonlyField cannot be edited but is submitted.
disabledField is disabled (not editable, not submitted).
multipleAllows multiple values (file upload, select).
checkedPre-selects a checkbox or radio button.
selectedPre-selects an option in a dropdown.
sizeSets width of input/select field.
stepDefines step interval for numeric/date input.
listConnects input with a <datalist> for suggestions.
typeDefines input type (text, email, password, etc.).
idUnique identifier, used with <label> and CSS/JS.

Example  : 

<form action="submit.php" method="post" autocomplete="on" novalidate>
  
  <!-- Text with placeholder & required -->
  <label for="name">Name:</label>
  <input type="text" id="name" name="username" placeholder="Enter your name" required><br><br>

  <!-- Email with pattern -->
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" placeholder="Enter email" pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" required><br><br>

  <!-- Number with min, max, step -->
  <label for="age">Age:</label>
  <input type="number" id="age" name="age" min="18" max="60" step="1" required><br><br>

  <!-- Password with minlength, maxlength -->
  <label for="pwd">Password:</label>
  <input type="password" id="pwd" name="password" minlength="6" maxlength="12" required><br><br>

  <!-- Submit -->
  <input type="submit" value="Register">
</form>

Output : 

learn code with durgesh images

Article 0 of 0