[PHP Code > PHP Radio+ Buttons Control]

Radio+ Buttons

Picture of me and my dog

This demo shows the use of the HTML Radio Button Control and how to use a single PHP variable to gather the user's input after a Submit. This also illustrates how to maintain and show what the user selected -- in other words, make the form sticky.

In addition, the code shown here uses an array to hold the fruit. As such, this method presents an alternate way to expand the items in the form by simply adding items to the fruit array. This is known as making code scalabel.

Finally, this code shows how you can make the code easier to read by echoing some of the html. YMMV

Apple
Pear
Orange
Mango
Apricot
Pineapple


Please note: I have NOT included HTML labels for input statements. This was done to simplify the code and concept. If you are going to create forms for your websites, then you should also use labels to identify the HTML controls for the disabled. Please review the demos provided -- these show lables for all input controls requiring user action.

You may use the following script as you need without any obligation to me whatsoever. However, a Donation via PayPal would be greatly appreciated -- please note, this site does not accept advertising.


Here's the PHP/HTML code for the "Radio+ Buttons" Control:

00 
07 <?php 08 09 error_reporting(E_ALL); // set error reporting to all 10 $self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME'])); 11 12 // populate the fruit array 13 $fruit = array("Apple", "Pear", "Orange","Mango", "Apricot", "Pineapple"); 14 15 if(isset($_POST['choice'])) 16 { 17 $choice = $_POST['choice']; 18 } 19 else 20 { 21 $choice = ''; // could set default here 22 } 23 ?> 24 25 25 <!doctype html> 25 <html lang="en-us"> 25 25 <head> 25 <meta charset="utf-8"> 25 <title>tedd's Examples</title> 25 </head> 25 25 <body> 26 54 <form action="<?php echo($self); ?>" method="POST" class="examples"> 55 56 <?php 57 58 foreach($fruit as $item) 59 { 60 $s = ($choice == $item) ? 'CHECKED' : ''; 61 echo("<input type=\"radio\" name=\"choice\" value=\"$item\" $s > $item<br>"); 62 } 63 ?> 64 65 <br> 66 <input type="submit" name="submit" value="Submit"> 67 </form> 68 68 </body> 68 </html>
  Show the above CODE for Copy/Paste below.

The following code will work "as-is" without any additional coding. However, remember to use a .php' suffix.


<?php error_reporting(E_ALL); // set error reporting to all $self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME'])); // populate the fruit array $fruit = array("Apple", "Pear", "Orange","Mango", "Apricot", "Pineapple"); if(isset($_POST['choice'])) { $choice = $_POST['choice']; } else { $choice = ''; // could set default here } ?> <!doctype html> <html lang="en-us"> <head> <meta charset="utf-8"> <title>tedd's Examples</title> </head> <body> <form action="<?php echo($self); ?>" method="POST" class="examples"> <?php foreach($fruit as $item) { $s = ($choice == $item) ? 'CHECKED' : ''; echo("<input type=\"radio\" name=\"choice\" value=\"$item\" $s > $item<br>"); } ?> <br> <input type="submit" name="submit" value="Submit"> </form> </body> </html>