PHP Form Hidden Field

In this chapter you will learn:

  1. What is form field
  2. Example - PHP Form Hidden Field

Description

A hidden field is not displayed on the page.

It simply stores the text value specified in the value attribute.

Hidden fields are great for passing additional information from the form to the server.


<label for="hiddenField">A hidden field</label> 
<input type="hidden" name="hiddenField" id="hiddenField" value="" /> 

Example

The following form uses hidden field to store number of tries.


<?php/*j a  v a  2 s .  co  m*/
  $num_to_guess = 42;
  $message = "";
  if (! isset ( $_POST ['guess'] )) {
    $message = "Welcome!";
  } else if ($_POST ['guess'] > $num_to_guess) {
    $message = $_POST ['guess'] . " is too big!";
  } else if ($_POST ['guess'] < $num_to_guess) {
    $message = $_POST ['guess'] . " is too small!";
  } else {
    $message = "Well done!";
  }
  $guess = ( int ) $_POST ['guess'];
  $num_tries = ( int ) $_POST ['num_tries'];
  $num_tries ++;
  ?>
<html>
<body>
 <?php print $message?>
 Guess number: <?php print $num_tries?><br />
<form method="post" action="<?php
print $_SERVER ['PHP_SELF']?>">
<input type="hidden" name="num_tries"
  value="<?php
  print $num_tries?>" /> Type your guess here: <input type="text" name="guess" value="<?php
  print $guess?>" />
</form>
</body>
</html>

Next chapter...

What you will learn in the next chapter:

  1. Why to split form across pages
Home » PHP Tutorial » PHP Form
PHP HTML Forms
PHP GET and POST
PHP Form Elements
PHP Form TextField
PHP Form Password Field
PHP Form Textarea
PHP Form CheckBox
PHP Form Select
PHP Form RadioButton
PHP Form Submit
PHP Form Reset
PHP Form Push Button
PHP Form Image
PHP Form Hidden Field
PHP Split Form Across Pages
PHP Form Validation
PHP Form File Upload
PHP Form Demo
PHP Form Empty Fields
PHP Form Multi-Value Fields
PHP Redirect after a Form Submission