PHP 单选按钮组变量


PHP radio button group variables

我已经简化了这个问题。但是,如果选中按钮组合,我需要做的就是显示不同的结果。目前,如果检查大米和茶,则没有结果。

<?PHP
if (isset($_POST['Submit1'])) {
    $selected_radio = $_POST['food']."|".$_POST['drink'];
        if ($selected_radio == 'rice' && 'tea') {
            print '<h1>Rice and tea</h1>';
        }
        else if ($selected_radio == 'beans' && 'coffee') {
            print '<h1>Beans and coffee</h1>';
        }
}
?>  

<form name ="form1" method ="POST" action =" ">
<p><input type = 'Radio' Name ='food'  value= 'rice'>Rice</p>
<p><input type = 'Radio' Name ='food'  value= 'beans'>Beans</p>

<p><input type = 'Radio' Name ='drink'  value= 'tea'>Tea</p>
<p><input type = 'Radio' Name ='drink'  value= 'coffee'>Coffee</p>

<input type = "Submit" Name = "Submit1"  value = "Select">
</form>

(很抱歉之前问过类似的问题)

如果条件更新

if (isset($_POST['Submit1'])) {
    $selected_radio = $_POST['food']."|".$_POST['drink'];
        if ($selected_radio == 'rice|tea') {
            print '<h1>Rice and tea</h1>';
        }
        else if ($selected_radio == 'beans|coffee') {
            print '<h1>Beans and coffee</h1>';
        }
}

更改 if 的条件:

    if (strpos($selected_radio, 'rice') !== FALSE && strpos($selected_radio, 'tea') !== FALSE) {
        print '<h1>Rice and tea</h1>';
    }
    else if (strpos($selected_radio, 'beans') !== FALSE && strpos($selected_radio, 'coffee') !== FALSE) {
        print '<h1>Beans and coffee</h1>';
    }

(这只是一个建议,我没有测试它;))

使用以下代码:

$selected_radio_list = [
    'rice|tea' => 'foo',
    'beans|coffee' =>'bar'
];  
$post = $_POST['food']. "|" .$_POST['drink'];  
if(in_array($post, $selected_radio_list )){
    echo $selected_radio_list[$post];
}