使用if回显基于单选按钮的语句和下拉列表选项PHP

时间:2012-10-20 22:53:07

标签: php drop-down-menu radiobuttonlist

我在互联网上寻找这个具体问题的答案,但似乎找不到答案。基本上,我有一组单选按钮和一个下拉列表,您可以从中选择选项。

<form method="get" action="phpindex.php">
    <select name="season">
        <option value="">- Season -</option>
        <option value="spring">Spring</option>
        <option value="summer">Summer</option>
        <option value="fall">Fall</option>
        <option value="winter">Winter</option>
    </select>
    <br /><br />
    <input type="radio" name="beer" value="heavy"/>Heavy
    <br />
    <input type="radio" name="beer" value="light"/>Light
    <br />
    <input type="submit" OnClick="show_alert"/>
</form>

根据您的选择组合,您可以在单击提交按钮时获得特定声明。例如,如果您选择春季作为季节,并选择浓啤酒,则应根据这些选择回应声明。问题是,回声功能不起作用,我认为这是因为单选按钮和下拉列表的组合。这是我的PHP和前面提到的echo函数,它们不起作用。

<?php

$beer = $_GET["beer"];
$season = $_GET["season"];
$spring = $_GET["sp"];
$summer = $_GET["summer"];
$fall = $_GET["fall"];
$winter = $_GET["winter"];
$heavy = $_GET["heavy"];
$light = $_GET["light"];


if ( in_array("spring", $season) && in_array("heavy", $beer)  ) {
echo "$state Bud"; } 
if ( in_array("spring", $season) && in_array("light", $beer) ) {
echo "$state Abita"; }
if ( in_array("summer", $season) && in_array("heavy", $beer) ) {
echo "$state Yuengling"; }
if ( in_array("summer", $season) && in_array("light", $beer) ) {
echo "$state Coors"; }
if ( in_array("fall", $season) && in_array("heavy", $beer) ) {
echo "$state PBR"; }
if ( in_array("fall", $season) && in_array("light", $beer) ) {
echo "$state Miller"; }
if ( in_array("winter", $season) && in_array("heavy", $beer) ) {
echo "$state Natty"; }
if ( in_array("summer", $season) && in_array("light", $beer) ) {
echo "$state Kona"; }

$state = "Well, it looks like it's $season, and you want a $beer beer, so try this brew out:";


?>

任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:0)

您希望将该字符串保存到if语句上方的$ state。

$state = .......

if....
if....

答案 1 :(得分:0)

首先,您必须先拥有$ state ='...'然后再使用if语句

$ _GET是一个数组,所以你可以做in_array,但$ _GET ['beer']或$ _GET ['season']不是一个数组。

所以$ beer和$ season不是你的情况下的数组。您可以轻松地将语句更改为:

$state = "Well, it looks like it's $season, and you want a $beer beer, so try this brew out:";
if ( $season == "spring" && $beer == "heavy"  ) {
    echo "$state Bud"; 
} 
....

依旧......

并且因为我认为你期待只有一个复制粘贴,所以你去了:

<?php

$beer = $_GET["beer"];
$season = $_GET["season"];
$spring = $_GET["sp"];
$summer = $_GET["summer"];
$fall = $_GET["fall"];
$winter = $_GET["winter"];
$heavy = $_GET["heavy"];
$light = $_GET["light"];

$state = "Well, it looks like it's $season, and you want a $beer beer, so try this brew out:";

if ( $season == "spring" && $beer == "heavy" ) {
    echo "$state Bud"; 
} 
if ( $season == "spring" && $beer == "light" ) {
    echo "$state Abita"; 
}
if ( $season == "summer" && $beer == "heavy" ) {
    echo "$state Yuengling"; 
}
if ( $season == "summer" && $beer == "light" ) {
    echo "$state Coors"; 
}
if ( $season == "fall" && $beer == "heavy" ) {
    echo "$state PBR";
}
if ( $season == "fall" && $beer == "light" ) {
    echo "$state Miller"; 
}
if ( $season == "winter" && $beer == "heavy" ) {
    echo "$state Natty"; 
}
if ( $season == "summer" && $beer == "light" ) {
    echo "$state Kona"; 
}

答案 2 :(得分:0)

那里有一些错误

首先,您只需要:

$beer = $_GET["beer"];
$season = $_GET["season"];

秋天,春天等是价值,而不是GET参数。 之后,检查GET的值以将啤酒anme存储在var;

if ($beer == 'heavy' && $season == 'spring') $brand= "Bud";
 if ($beer == 'light' && $season == 'spring') $brand= "Abita";
 //ad so on....

$state = "Well, it looks like it's $season, and you want a $beer beer, so try this brew out: $brand";
echo $state;

你不能回显$ state,因为你的回音调用后定义了$ state。 相反,只需将brew放入var中,然后将其附加到所有if后的状态字符串中。

欢呼声。

答案 3 :(得分:-1)

以这种方式使用的

$_GET元素只能是简单的数据类型,而不是数组,因为查询字符串的每个段(例如q=v)都被定义为表示单个值v - q数组中的键$_GET

如下所述,当"array[]"用于name标记的input属性时会出现例外情况,在此特定情况下,$_GET标记会在$_POST标记中创建数组{1}} / if ($season == "spring" && $beer == "heavy")

在您的情况下,您希望直接将值作为字符串进行测试:

{{1}}

例如。