Change value of radio button based on PHP code.

I have multiple radio buttons that are linked together. I want to set the value of each radio button based on whether it is clicked/set or not

    <td><br><input type="radio" name="meal1" <?php if (isset($meal1)){
        echo "checked", "value = '1'";
    } else {
        echo "value = '0'";
    }
    ?>>
    </td>

    <td><br><input type="radio" name="meal1" <?php if (isset($meal2)){
        echo "checked", "value = '1'";
     } else {
        echo "value = '0'";
     }
     ?>>
    </td>
                       
    <td><br><input type="radio" name="meal1" <?php if (isset($meal3)){
        echo "checked", "value = '1'";
    } else {
        echo "value = '0'";
    }
    ;?>>
    </td>
                        
    <td><br><input type="radio" name="meal1" <?php if (isset($meal4)){
        echo "checked", "value = '1'";
    } else {
        echo "value = '0'";
    }
    ;?>>
    </td>

This code is just to show the output of each of the radio buttons

    if(isset($_POST['order'])){
    if(!isset($_POST['meal1'])){
        echo "Radio buttons not set.";
    } else {
    $meal1 = $_POST['meal1'];
    $meal2 = $_POST['meal1'];
    $meal3 = $_POST['meal1'];
    $meal4 = $_POST['meal1'];
    
    print_r($meal1);
    print_r($meal2);
    print_r($meal3);
    print_r($meal4);
    }

When I execute the code below, after clicking on of the radio buttons. The output is 0,0,0,0. I would like the output to 1,0,0,0 Since I have clicked the first radio button.

Seems like you are using the value field wrong.

Radio buttons with the same name are linked together, and the value of the checked radio button will be submitted to the server.

ie:

[code]
Meal 1

Meal 2

[/code]

Checking “Meal 1” will add a “checked” param to the input. If you then submit the form you will receive $_POST[‘meal’] that will hold the value: 1.

[hr]

This code just sets $meal1/2/3/4 to the same value.
[php] $meal1 = $_POST[‘meal1’];
$meal2 = $_POST[‘meal1’];
$meal3 = $_POST[‘meal1’];
$meal4 = $_POST[‘meal1’];[/php]

Thank you for explaining. This is a lot easier than what I was doing. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service