首页 > 解决方案 > 验证文件找不到表单数据

问题描述

我已经制作了两个文件 php,一个有验证 php 代码,另一个有我输入一些数据的表单,但看起来 Switch 命令有问题,看了很多次才发现问题,但不幸的是我没有得到 true .

这是我的代码;

我已经形成的第一个文件

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

<label>Your favourite fruit:<input type="text" name="t_Opst" id="t_Opst"></label>
<input type="submit" name="G_mby" id="G_mby">
</form>

我有验证文件的第二个文件(php)

<?php

if(isset($_P0ST["G_mby"])){

$L_ings=$_POST["t_Opst"];

switch($L_ings){

case "Apple":

 echo "Your favourite fruit is $L_ings";
break;

case "Pear":


echo "Your favourite fruit is $L_ings";
break;

case "Banana":

 echo "Your favourite fruit is $L_ings";
break;

case "Mango":


 echo "Your favourite fruit is $L_ings";
break;

default:

echo "You should eat some fruit";

}
}
?>

标签: php

解决方案


在您的 php 文件中,您可以验证是否有 POST 请求,当涉及到您的代码时,不需要切换,因为相同的代码会重复多次,您可以使用 if 和 or 子句代替。尝试这个:

<?php

  if ($_SERVER["REQUEST_METHOD"] == "POST"){

    $L_ings = $_POST["t_Opst"];

    echo "hola";

    if($L_ings == "Pear" or $L_ings == "Banana" or $L_ings == "Mango" or $L_ings == "Apple")
         echo "Your favourite fruit is $L_ings";
    else
         echo "You should eat some fruit";
  }

?>

推荐阅读