PHP HTML HiddenField

From CoderLearner
Jump to: navigation, search

PHP works with HTML hidden field

HTML hidden field can be used to pass information from and to the web page.

Syntax hidden

<input type="hidden" name="name" value="the value"/>

Note

Hidden field does not display on a web browser, but its value can be found by viewing the HTML source. It is important not to pass critical information through the hidden field.

Example 1

<?php
if ($_POST) {
   print_r($_POST);
}
?>
<html>
   <head>
      <title>Hidden Field</title>
   </head>
   <body>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
         <table border="0">
            <tr>
               <td>Name:</td>
               <td><input type="text" name="name" size="20"/></td>
            </tr>
            <tr>
               <td>Country:</td>
               <td><input type="text" name="country" size="30" /></td>
            </tr>
            <tr>
               <td colspan="2"><input type="submit" value="submit" /></td>
            </tr>
         </table>
         <input type="hidden" name="special_code" value="4762043656x42yJe245" />
      </form>
   </body>
</html>

Try It

Example 2

<?php
// process user input
if (!empty($_POST['name']) && !empty($_POST['country'])) {

   $greeting = "Hello! "
           . ucfirst($_POST['name'])
           . ', you are from '
           . ucfirst($_POST['country']);

   $greeting .= ', your special code is (hidden field) '
           . $_POST['special_code'];
} else {
   $greeting = 'please enter your name and your home country!<br/>';
}
?>

<html>
   <head>
      <title>Hidden Field</title>
   </head>
   <body>
      <b><?php echo $greeting; ?></b>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
         <table border="0">
            <tr>
               <td>Name:</td>
               <td><input type="text" name="name" size="20"/></td>
            </tr>
            <tr>
               <td>Country:</td>
               <td><input type="text" name="country" size="30" /></td>
            </tr>
            <tr>
               <td colspan="2"><input type="submit" value="submit" /></td>
            </tr>
         </table>
         <input type="hidden" name="special_code" value="4762043656x42yJe245" />
      </form>
   </body>
</html>

Try It