php switch语句使用文本框/下拉框

Geo*_*rge -2 php switch-statement

我正在尝试使用switch语句使其在文本框中键入特定单词时将显示该车的可用模型

例如

如果用户输入"Volkswagen",它应该回应"可用型号是Beetle和Polo"

但由于某种原因它没有工作可以任何人建议我为什么?

这是我到目前为止的代码,

<form action="switch.php" method="post">

    <input type="text" name="cars" id="cars" />
       <input type="submit" /> 
<?php
$i = $_POST;
?>

<?php
switch ($i) {
    case "Volkswagen":
        echo "The available models are Beetle and Polo";
        break;
    case "Renault":
        echo "The Available models are Megane and Clio";
        break;
    case "Land Rover":
        echo "The Available models are Range Rover Sport and Defender";
        break;
}
?>
</form> 
Run Code Online (Sandbox Code Playgroud)

编辑:

我现在有点卡住我试图使用带有下拉框的这个开关语句,并且不太确定如果有人可以帮助我让它们一起工作那么我可以如何让它们一起工作

这是我的工作开关和工作下拉框的代码.

<form action="">
<select name="cars">
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Land Rover">Land Rover</option>
</select>


<p>
<?php
switch($_POST['cars']) {
    case "Volkswagen":
        echo "The available models are Beetle and Polo";
        break;
    case "Renault":
        echo "The Available models are Megane and Clio";
        break; 
    case "Land Rover":
        echo "The Available models are Range Rover Sport and Defender";
        break;
}
?>
</p>
</form>
</center>
Run Code Online (Sandbox Code Playgroud)

Mar*_*c B 6

$_POST是一个数组.这不是你可以与字符串比较的东西.PHP会将数组转换为文字Array.由于您没有该单词的案例,因此根本没有匹配.

表单值作为$ _POST数组的元素提交.你应该这样做

switch($_POST['cars']) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

代替