-1

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

<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

4 回答 4

0

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

$state = .......

if....
if....
于 2012-10-20T22:58:45.747 回答
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"; 
}
于 2012-10-20T23:02:22.203 回答
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 之后将其附加到您的状态字符串。

干杯。

于 2012-10-20T23:07:33.717 回答
-1

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

正如下面所指出的,当"array[]"用于标记的name属性时有一个例外input,在这种特定情况下,它会在$_GET/中创建一个数组$_POST

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

if ($season == "spring" && $beer == "heavy")

例如。

于 2012-10-20T23:04:05.590 回答