[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.

Apple
Pear
Orange
Mango
Apricot


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 $s = 'CHECKED'; 13 14 if(isset($_POST['choice'])) 15 { 16 $choice = $_POST['choice']; 17 } 18 else 19 { 20 $choice = ''; // could set default here 21 } 22 ?> 23 24 24 <!doctype html> 24 <html lang="en-us"> 24 24 <head> 24 <meta charset="utf-8"> 24 <title>tedd's Examples</title> 24 </head> 24 24 <body> 25 40 <form action="<?php echo($self); ?>" method="POST" class="examples"> 41 <p> 42 <input type="radio" name="choice" value="Apple"<?php if ($choice == 'Apple') echo($s); ?> > Apple<br> 43 <input type="radio" name="choice" value="Pear" <?php if ($choice == 'Pear') echo($s); ?>> Pear<br> 44 <input type="radio" name="choice" value="Orange" <?php if ($choice == 'Orange') echo($s); ?> > Orange<br> 45 <input type="radio" name="choice" value="Mango" <?php if ($choice == 'Mango') echo($s); ?> > Mango<br> 46 <input type="radio" name="choice" value="Apricot" <?php if ($choice == 'Apricot') echo($s); ?> > Apricot<br> 47 </p> 48 <p> 49 <input type="submit" name="submit" value="Submit"> 50 </p> 51 </form> 52 52 </body> 52 </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'])); $s = 'CHECKED'; 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"> <p> <input type="radio" name="choice" value="Apple"<?php if ($choice == 'Apple') echo($s); ?> > Apple<br> <input type="radio" name="choice" value="Pear" <?php if ($choice == 'Pear') echo($s); ?>> Pear<br> <input type="radio" name="choice" value="Orange" <?php if ($choice == 'Orange') echo($s); ?> > Orange<br> <input type="radio" name="choice" value="Mango" <?php if ($choice == 'Mango') echo($s); ?> > Mango<br> <input type="radio" name="choice" value="Apricot" <?php if ($choice == 'Apricot') echo($s); ?> > Apricot<br> </p> <p> <input type="submit" name="submit" value="Submit"> </p> </form> </body> </html>