PHP: Creating a group of radio buttons
Following on from the previous post is this tutorial. This one is very similar except this time we’re going to do the same trick with some radio buttons. The same principles as last time apply – we’ll read the values we want into an array and then use foreach to echo them out.
Radio buttons aren’t too much different to select boxes. Only one of the options in the group can be chosen, and only one can be preselected. Since we’ve already established the basics in the dropdown tutorial, we’ll just jump in boots and all and start rendering our output. The HTML output is slightly different – radio buttons have no containing element as the select does – they’re bound together by having a common name element.
foreach ($countries as $c){ echo '<input type="radio" name="country" value="'.$c['id'].'"> '; echo $c['country'].', '.$c['capital'].'<br />';}
Easy! The result of this script is:
<input type="radio" name="country" value="1"> New Zealand, Wellington<br /><input type="radio" name="country" value="2"> Australia, Canberra<br /><input type="radio" name="country" value="3"> England, London<br /><input type="radio" name="country" value="4"> United States, Washington D.C.<br /><input type="radio" name="country" value="5"> France, Paris<br /><input type="radio" name="country" value="6"> Italy, Rome<br /><input type="radio" name="country" value="7"> Malaysia, Kuala Lumpur<br />
And that displays in your browser like so:
New Zealand, WellingtonAustralia, Canberra
England, London
United States, Washington D.C.
France, Paris
Italy, Rome
Malaysia, Kuala Lumpur
Regarding preselecting an option, we do the same thing as we did in the dropdown by testing the ID value of the current array element ($c) with the value of the ID passed into the script ($_GET['id']).
foreach ($countries as $c){ if ($c['id'] == $_GET['id']) echo '<input type="radio" name="country" value="'.$c['id'].'"> '; else echo '<input checked="true" type="radio" name="country" value="'.$c['id'].'"> ';
echo $c['country'].', '.$c['capital'].'<br />';}
Done! The source for this is available right here.
3 Comments
Other Links to this Post
RSS feed for comments on this post. TrackBack URI
By Code Nexus, June 26, 2006 @ 12:44 pm
Pity the blogger theme, places your code out into the menus on the right. Not your fault though. How about installing wordpress or something on your server?
Ever thought of writing a book?? Guru Bobs Practicle PHP…Oooh! I can see a series here… kind of like the “… for Dummies” books. Guru Bobs Helpful HTML, Guru Bobs Meaningful MySQL and Guru Bobs Delightful Documentation.
By Code Nexus, August 22, 2006 @ 6:00 pm
OK took a look back at this post and realised that you may be making a bit of a bad call. I can see where the “method” may come in handy but surely you wouldn’t want to have this in the database and call it all the time on a high demand site. This country list is something that perhaps is best left in hard coded as it isn’t very dynamic.
Other than that its ok…
By Bruhaha, September 8, 2006 @ 8:47 am
thanks for the post. very helpful.