Creating Buttons: Take 2


In the previous chunk, we used a mixture of JavaScript and a hidden control to determine which button of a set of three a user clicked in a web page. However, you can do the same thing without any JavaScript if you use three Submit buttons as your buttons.

To do that, you use three different HTML forms in your web page, each with its own Submit button. To the user, it looks as if you have three simple push buttons in your page, but when the user clicks one of the buttons, that form's data is sent back to your PHP script:

 <FORM NAME="form1" ACTION="phpbuttons.php" METHOD="POST">     <INPUT TYPE="SUBMIT" VALUE="Button 1"> </FORM> 

To send some data back to the PHP script, we'll add a hidden control to each of the three forms, containing the name of the clicked button, as shown in phpbuttons2.html, Example 5-21.

Example 5-21. Using buttons: take 2, phpbuttons2.html
 <HTML>     <HEAD>         <TITLE>Using Buttons: Take 2</TITLE>     </HEAD>     <BODY>         <H1>Using Buttons: Take 2</H1>         <FORM NAME="form1" ACTION="phpbuttons.php" METHOD="POST">             <INPUT TYPE="HIDDEN" NAME="Button" VALUE="button 1">             <INPUT TYPE="SUBMIT" VALUE="Button 1">         </FORM>         <FORM NAME="form2" ACTION="phpbuttons.php" METHOD="POST">             <INPUT TYPE="HIDDEN" NAME="Button" VALUE="button 2">             <INPUT TYPE="SUBMIT" VALUE="Button 2">         </FORM>         <FORM NAME="form3" ACTION="phpbuttons.php" METHOD="POST">             <INPUT TYPE="HIDDEN" NAME="Button" VALUE="button 3">             <INPUT TYPE="SUBMIT" VALUE="Button 3">         </FORM>         </SCRIPT>     </BODY> </HTML> 

This page appears in Figure 5-21, where you can see the three Submit buttons masquerading as three simple push buttons.

Figure 5-21. Determining which button was clicked, Take 2.


To read the name of the clicked button in phpbuttons2.php, Example 5-22, just extract the text from the hidden control, "Button".

Example 5-22. Reading buttons: take 2, phpbuttons2.php
 <HTML>     <HEAD><TITLE>Using Buttons: Take 2</TITLE></HEAD>     <BODY><CENTER>         <H1>Using Buttons: Take 2</H1>         You clicked         <?php             if (isset($_REQUEST["Button"]))                 echo $_REQUEST["Button"], "<BR>";         ?>     </CENTER></BODY> </HTML> 

The results appear in Figure 5-22, where we've been able to determine the clicked button.

Figure 5-22. Indicating the clicked button, Take 2.


Here we've used hidden controls to store our data, but in fact there's an even simpler technique where you don't need them at all.



    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