PHP单选按钮组变量

时间:2015-01-20 16:22:07

标签: php radio-button

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

<?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>

(早先提出类似问题道歉)

3 个答案:

答案 0 :(得分:2)

条件

时更新
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>';
        }
}

答案 1 :(得分:1)

更改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>';
    }

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

答案 2 :(得分:0)

  

$ selected_radio_list = array(&#39; rice | tea&#39; =&gt;&#39; foo&#39;,&#39;豆|咖啡&#39; =&gt;&#39; bar&#39 );

     

$ post = $ _POST [&#39; food&#39;]。&#34; |&#34;。$ _ POST [&#39; drink&#39;];

     

if(in_array($ post,$ selected_radio_list))

     

echo $ selected_radio_list [$ post];