Working with forms in PHP - Radio Buttons
(Page 5 of 7 )
Radio buttons are very similar to checkboxes. The only real difference is that when you call radio buttons by the same name, only one of them can be selected. That really makes our life easier, as we don't have to loop through and figure out which ones are selected. Add this next to form.html:
Choose one: Blue <INPUT type="radio" name="myradio" value="blue" CHECKED> Green <INPUT type="radio" name="myradio" value="green"> Yellow <INPUT type="radio" name="myradio" value="yellow"><BR> |
And to form.php:
echo "<BR>The color you picked was " . $_POST['myradio'] . "\n"; |
Very straightforward, just like the others.
Select BoxesOn to select boxes or as they are sometimes called, drop-down lists. The way these work is exactly the same as the other elements we have covered so far. Really, once you know one, you know them all! In form.html, add these lines next:
Select something from this list: <SELECT name="myselectbox"> <OPTION value="dog">Dog</OPTION> <OPTION value="cat">Cat</OPTION> <OPTION value="pig">Pig</OPTION> </SELECT><BR> |
And in the form.php script add this next:
echo "<BR>" . $_POST['myselectbox'] . " was chosen from the select list.\n"; |
Select boxes are just as simple as that. One thing to note is that if you do this:
<OPTION value="cat">Dog</OPTION> |
cat is what will be passed to the form.php script. Dog is only there to display, cat is the value assigned to that item in the list. This can be useful if you want to display a list of items to a user, but assign a numerical value to this items, like:
<SELECT name="myselectbox"> <OPTION value="1">One thing</OPTION> <OPTION value="2">Another thing</OPTION> <OPTION value="3">Something totally different</OPTION> </SELECT><BR> |
Next: Textareas >>
More Miscellaneous Articles
More By Matt Wade
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|