HTML5 Intermediate Accessible Forms · 6 min read

Hidden Fields and What Not to Trust

Lesson 35 of 36 in the HTML5 learning path.

What is a hidden input for, and what must the server ignore?

A hidden input submits a value without showing a control. Anyone can edit it. Hidden does not mean secret.

Use it for a method override or a CSRF token. Owner and price come from the session and the database.

Syntax & Parameters

Syntax
<input type="hidden" name="_method" value="put">

Example

Interactive preview available
edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Edit</title>
<style>
body{font:1rem/1.5 sans-serif;margin:2rem}
</style>
</head>
<body>
<form method="post" action="/lessons/12"><input type="hidden" name="_method" value="put"><label for="title">Title</label><input id="title" name="title" value="Routes"><button name="action" value="save">Save</button><button name="action" value="preview">Preview</button></form>
</body>
</html>

Expected Output

Save posts _method=put, title, and action=save. A tampered price field is not in this form and must be dropped if added.

Line-by-Line Walkthrough

  1. The form posts to the lesson URL.
  2. The hidden field asks the framework to treat the POST as PUT.
  3. Title is the only content the user should edit.
  4. Each submit button sends a different action value.
  5. The server chooses a path from action.
  6. The server loads the lesson from the URL.
  7. A posted user id is ignored.
  8. CSRF tokens are hidden fields the server compares to the session.

Common Pitfalls & Security

Hidden fields are user input.

  • A hidden user id lets the client impersonate someone if you honor it.
  • A hidden price lets the client set the charge.
  • Secrets in hidden fields show up in view source.
  • Disabled fields are not submitted. Hidden fields are.

Best Practices & Modern Standards

Limit hidden fields to tokens and method overrides you verify.

Name submit buttons when one form has two actions.

Real-World Use

The edit form posts a method override and the new title. The controller checks the policy and never reads a hidden owner.

Frequently Asked Questions

Are hidden fields secure?

No. They are only visually hidden.

When are they appropriate?

For a CSRF token or a method override, not a price.

Do disabled inputs submit?

No. Hidden inputs do.

How do two buttons differ?

Give them the same name and different values.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…