Creating Buttons: Take 3


We've seen two ways to handle push buttons in web pages, but there's a third way; you can pass data back to a PHP script using the VALUE attribute of Submit buttons, which can be read in a PHP script. This technique avoids having to use a hidden control to send data back to your script.

To do this, just give each Submit button the same name, "Button" in this next example, and assign the name of the button to the VALUE attribute in each of the three forms:

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

You can see how this looks in a web page in phpbuttons3.html, Example 5-23.

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

As you see in Figure 5-23, all three buttons are visible, as before, in the browser.

Figure 5-23. A set of buttons, Take 3.


In the PHP script, phpbuttons3.php, Example 5-24, all you need to do is the check value of the control named "Button"which this time is a Submit button, not a hidden field.

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

As before, the PHP script can determine which button was clicked, as you see in Figure 5-24. Presto.

Figure 5-24. Indicating the clicked button, Take 3.




    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