Php 使用基于单选按钮和下拉列表选项的if-to-echo语句

Php 使用基于单选按钮和下拉列表选项的if-to-echo语句,php,drop-down-menu,radiobuttonlist,Php,Drop Down Menu,Radiobuttonlist,我在互联网上到处寻找这个问题的答案,但似乎找不到答案。基本上,我有一套单选按钮和一个下拉列表,你可以从中选择选项 <form method="get" action="phpindex.php"> <select name="season"> <option value="">- Season -</option> <option value="spring">Spring</option&

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

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

-时令-
春天
夏天
落下
冬天


沉重的
轻的
根据您的选择组合,单击submit按钮后,您将得到一个特定的语句来响应。例如,如果你选择春天作为季节,喝一杯浓烈的啤酒,那么就应该根据这些选择做出回应。问题是,回音功能不起作用,我认为这是因为单选按钮和下拉列表的组合。下面是我的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:";


?>

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

$state = .......

if....
if....

首先,您必须首先使用$state='…',然后使用if语句

$\u-GET是一个数组,因此可以在\u数组中执行,但是$\u-GET['beer']或$\u-GET['seasure']不是数组

因此,在您的案例中,$啤酒和$季节不是一个数组。您可以轻松地将您的声明更改为:

$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"; 
}

$\u GET
以这种方式使用的元素只能是简单的数据类型,而不是数组,因为查询字符串的每一段(例如
q=v
)都定义为表示单个值
v
——在
$\u GET
数组中使用键
q

如下所述,当
输入
标记的
名称
属性使用
“array[]”
时,有一个例外,在这种特定情况下,该标记在
$\u GET
/
$\u POST
中创建一个数组

在您的情况下,您希望直接以字符串形式对值进行测试:

if($seasure==“spring”&&$beer==“high”)


例如。

这里有一些错误

首先,您需要的是:

$beer = $_GET["beer"];
$season = $_GET["season"];
秋天、春天等都是有价值的,而不是有价值的。 之后,检查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之后将其附加到状态字符串中


干杯。

在数组中()
用于检查数组是否包含值。不过,
$\u GET
参数不是数组。一个简单的
==
就足够了。啊,比我想象的简单多了!我是新手,所以还在学习诀窍。谢谢!哈哈,我不介意自己做些改变,但是谢谢!没有意识到更改需要多么小。像
这样的元素将在
$\u GET
$\u POST
超全局中生成数组。@LinusKleen哦,对了,谢谢,你每天都能学到新东西。尽管这是一个模糊的特性(或者,至少,在半常规的PHP开发6年之后,我从来没有遇到过这种特性!),我可能不会急于使用它。编辑了我的答案,以反映我现在理解的情况。