A Drupal form hidden field example

Drupal form FAQ: How do I create an HTML hidden field in a Drupal form (a Drupal 6 or Drupal 7 form)?

Assuming that you're comfortable with creating a Drupal form, you can create a Drupal form hidden field like this:

# an html hidden field for our drupal form
$form['wizard_page'] = array(
  '#type' => 'hidden',
  '#value' => 2,
);

The "type=hidden" declaration in this Drupal form array element is what tells Drupal that we want to create a hidden form field with the given property.

That Drupal hidden field declaration creates an HTML hidden field in your Drupal form that looks like this:

<input type="hidden" name="wizard_page" id="edit-wizard-page" value="2"  />

Other Drupal form hidden field references

(If you're not comfortable with creating a Drupal module or Drupal form, please see my first Drupal form example. You can find more information about the Drupal hidden field property on the this Drupal Form API reference page.

HTML hidden fields aren't really hidden

As an important note, if you're new to HTML programming, "hidden" fields aren't completely hidden; they are sent to the user's web browser, but the browser knows it shouldn't render that field. As a result, the user can always see the value of a "hidden" form field by selecting the "View Source" option in their browser. Therefore, you should never use the hidden form field for anything that's really important. You should keep information like that in the user's session, or perhaps use the Drupal value field instead.

Drupal form hidden field example - Summary

I hope this Drupal form "hidden field" example has been helpful. If you have any questions just leave a note in the Comments section below and I'll be glad to help.