- PHP

Switch and Case for Radio Button in PHP

In many cases, one of the best ways to parse the data coming from the radio button is to use a switch statement.

Here is an example:

<form action="" method="POST">
    <label><input type="radio" name="method" value="1"> Method 1</label>
    <label><input type="radio" name="method" value="2"> Method 2</label>
    <label><input type="radio" name="method" value="3" disabled=""> Method 3</label>

    <br>

    <input type="submit">
</form>

<?php 

if (!empty($_POST)) {

    switch ($_POST['method']) {
        case '1':
            // do something
            echo 'Picked method: 1';
            break;

        case '2':
            // do something
            echo 'Picked method: 2';
            break;
        
        case '3':
            // do something
            echo 'Method 3 is not available yet.';
            break;
          
    }

}

?>