PHP: Radio button me

This entry is part 45 of 54 in the series PHP Tutorial

In a typical button form, the HTML for a checked radio button will look like:

    <input type='radio' name='radio_button' value='ipad'>iPad
    <input type='radio' name='radio_button' value='camera' checked>Camera
    <input type='button' value='Submit' onclick=''>

Notice the checked word that will turn the button into a checked button.

Below we look at a way where a button could be unchecked and checked to see how PHP actually helps in modifying the HTML code.

Radio Button
$ipad_status = 'unchecked';
$iphone_status = 'checked';

if (isset($_POST['submit_name'])) {
    if ($ipad_status == 'checked') {
        $ipad_status = 'unchecked';
        $iphone_status = 'checked';
    } elseif ($iphone_status == 'checked') {
        $iphone_status = 'unchecked';
        $ipad_status = 'checked';

<form name="myform" method="post" action="radiome.php">
    <?php ?>
    <Input type='radio' Name='gadget' value='ipad'
        <?php echo $ipad_status ?>

    <Input type='radio' Name='gadget' value='iphone'
        <?php echo $iphone_status ?>
        <Input type="submit" Name="submit_name" value="Radio Button Me">


You can try the radio button here.  Click on Radio Button Me.  Once you click on Radio Button Me, the checked button will be moved to ipad.

In the code above, in line 23, the checked word, similar to line 4 in the top HTML form, is modified directly by an echo statement in <?php echo $iphone_status ?>.

Series Navigation<< PHP: Radio button form
PHP: Is the checkbox checked? >>