Creating Hidden Controls


Another handy control type in PHP is the hidden control, which lets you store hidden text data in web pages. This is useful when you want to store data about a user and the user doesn't allow cookies to be stored, for instance.

Here's an example, phphidden.html, Example 5-11, which contains the text "No worries." in a hidden control named "Hidden".

Example 5-11. Creating hidden data, phphidden.html
 <HTML>     <HEAD>         <TITLE>             Using Hidden Controls         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Using Hidden Controls             </H1>             <FORM METHOD="POST" ACTION="phphidden.php">                 Click the button to see the hidden data.                 <INPUT NAME="Hidden" TYPE="HIDDEN" VALUE="No worries.">                 <BR>                 <BR>                 <INPUT TYPE="SUBMIT" VALUE="Click Me">             </FORM>         </CENTER>     </BODY> </HTML> 

You can see this page, phphidden.html, in Figure 5-11. The data in the hidden control isn't visibleobviouslybut when you click the button, the PHP script phphidden.php will retrieve that data and display it.

Figure 5-11. Using hidden controls.


You can retrieve the data from a hidden control as you'd expectby name, using $_GET or $_POST as appropriate, or by using $_REQUEST. You can see how that works in phphidden.php, Example 5-12, where we're recovering the data in the hidden control as $_REQUEST["Hidden"].

Example 5-12. Retrieving data from hidden controls, phphidden.php
 <HTML>     <HEAD>         <TITLE>             Retrieving Hidden Data         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>Retrieving Hidden Data</H1>             The hidden data was:             <BR>             <?php                 echo $_REQUEST["Hidden"];             ?>         </CENTER>     </BODY> </HTML> 

The results appear in Figure 5-12, where we've been able to read the data from the hidden control and echo it in a web page.

Figure 5-12. Reading data from hidden controls.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net