Creating Radio Buttons


Users can check as many checkboxes as they want at once, but if you use radio buttons instead, the user can select only one of them at a time. That's useful if you want to present a set of exclusive options, such as the current day of the week or whether the user is ordering a pizza, sandwich, or calzone.

You group radio buttons together by giving two or more the same name, as you see in phpradio.html, Example 5-7.

Example 5-7. Radio buttons, phpradio.html
 <HTML>     <HEAD>         <TITLE>Using Radio Buttons</TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>Using Radio Buttons</H1>             <FORM METHOD="POST" ACTION="radio.php">                 Would you like cash back?                 <INPUT NAME="Radio1" TYPE="RADIO" VALUE="Yes">                 Yes                 <INPUT NAME="Radio1" TYPE="RADIO" VALUE="No">                 No                 <BR>                 <BR>                 <INPUT TYPE="SUBMIT" VALUE="Submit">             </FORM>         </CENTER>     </BODY> </HTML> 

And you can see phpradio.html at work in Figure 5-7.

Figure 5-7. Creating radio buttons.


To recover the radio button that was selected in the radio button group, you use the name of the group with $_REQUEST, instead of having to work with each individual control as we did with checkboxes. You can see how this works in radio.php, Example 5-8.

Example 5-8. Reading data from radio buttons, phpradio.php
 <HTML>     <HEAD>         <TITLE>Using Radio Buttons</TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>Retrieving Data From Radio Buttons</H1>             <?php                 echo "You selected ", $_REQUEST["Radio1"];             ?>         </CENTER>     </BODY> </HTML> 

The results appear in Figure 5-8, where we have indeed determined which radio button in the group the user selected.

Figure 5-8. Reading data from radio buttons.


What if you want multiple sets of radio buttons? No problemjust give each set of radio buttons its own name with the NAME attribute.



    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