PHP isset 函数 returns 表单中的所有选项

PHP isset function returns all options from form

有人可以帮助我学习 PHP 吗?我想创建简单的表单,添加 'isset function' 后返回所有表单。

<html>
<body>
<div id="main">
    <form action = "" method = 'POST'>
    <select name = "something">
    <option value = "Monitors">Monitors</option>
    <option value = "Graphics">Graphics</option>
    <option value = "Peripherials">Peripherials</option>
    <option value = "Processors">Processors</option>
    </select>
    <input type = "submit" value = "OK">
    </form>
</div>


<?php


if (isset($_POST["something"]) == "Monitors")
{   
    echo "Monitors <br />";
}

if (isset($_POST["something"]) == "Graphics")
{   
    echo "Graphics <br />";
}

if (isset($_POST["something"]) == "Peripherials")
{   
    echo "Peripherials <br />";
}

if (isset($_POST["something"]) == "Processors")
{   
    echo "Processors <br />";
}

?>


</body>
</html>

它 returns 所有 'echo' 在选择表格中的一个选项后。 不使用 'isset' 是可以的,但是使用 'undefined variable' 会出现 returns 错误。 我哪里弄错了?

isset returns 布尔值,改为这样做;我所做的是检查 $_POST["something"] 是否已设置并且 $_POST["something"] 是 select 选项值之一

<html>
<body>
<div id="main">
    <form action = "" method = 'POST'>
    <select name = "something">
    <option value = "Monitors">Monitors</option>
    <option value = "Graphics">Graphics</option>
    <option value = "Peripherials">Peripherials</option>
    <option value = "Processors">Processors</option>
    </select>
    <input type = "submit" value = "OK">
    </form>
</div>


<?php


if (isset($_POST["something"]) && $_POST["something"] == "Monitors")
{   
    echo "Monitors <br />";
}

if (isset($_POST["something"])  && $_POST["something"]  == "Graphics")
{   
    echo "Graphics <br />";
}

if (isset($_POST["something"])  && $_POST["something"]  == "Peripherials")
{   
    echo "Peripherials <br />";
}

if (isset($_POST["something"])  && $_POST["something"]  == "Processors")
{   
    echo "Processors <br />";
}

?>


</body>
</html>

原因:- 基于PHP: isset - Manual

Returns TRUE if var exists and has value other than NULL. FALSE otherwise.

因此,您的 if 条件变为:-

if(true == 'Monitors'){

始终被视为 true 并且所有内容都在打印。

解决方法:-

改变if条件如下:-

if(isset($_POST["something"])  &&  $_POST["something"]== "Monitors"){

其他人依此类推。

另外你的 Php 代码没有必要长,像下面这样缩短:-

<?php
  if (isset($_POST["something"])){   
    echo $_POST["something"]; // add ."<br />" if you needed
  }
?>

所以完整的代码需要是:-

<html>
<body>
<div id="main">
    <form action = "" method = 'POST'>
    <select name = "something">
    <option value = "Monitors">Monitors</option>
    <option value = "Graphics">Graphics</option>
    <option value = "Peripherials">Peripherials</option>
    <option value = "Processors">Processors</option>
    </select>
    <input type = "submit" value = "OK">
    </form>
</div>

<?php
  if (isset($_POST["something"])){   
    echo $_POST["something"]; // add ."<br />" if you needed
  }
?>

尝试使用 Switch 语句

  <?php

    if(isset($_POST['something'])){
        $var =$_POST["something"];

        switch ($var) {
            case "Monitors":
                echo "Monitors!";
                break;
            case "Graphics":
                echo "Graphics!";
                break;
            case "Peripherials":
                echo "Peripherials";
                break;
            default:
                echo "Your favorite color is neither red, blue, nor green!";
        }
    }


    ?>

如前所述,isset 的结果是一个布尔值 - 可以在逻辑运算中对其进行测试(即:if true then )~ switch 语句使多个更干净if/then/else 条件

if( isset( $_POST['something'] ) ){
    switch( $_POST['something'] ){
        case 'Monitors':$item='Monitors';break;
        case 'Graphics ':$item='Graphics ';break;
        case 'Peripherials ':$item='Peripherials ';break;
        case 'Processors ':$item='Processors ';break;
    }
    printf('%s<br />', $item);/* use $item somewhere/somehow*/
}

你应该使用 if (isset($_POST["something"]) and $_POST["something"]== "Monitors")

这样的条件

因为 isset() return truefalse 作为 return 值。 请在下面找到更新代码。

<html>
    <body>
    <div id="main">
        <form action = "" method = 'POST'>
        <select name = "something">
        <option value = "Monitors">Monitors</option>
        <option value = "Graphics">Graphics</option>
        <option value = "Peripherials">Peripherials</option>
        <option value = "Processors">Processors</option>
        </select>
        <input type = "submit" value = "OK">
        </form>
    </div>


    <?php


    if (isset($_POST["something"]) and  $_POST["something"]== "Monitors")
    {   
        echo "Monitors <br />";
    }

    if (isset($_POST["something"]) and  $_POST["something"] == "Graphics")
    {   
        echo "Graphics <br />";
    }

    if (isset($_POST["something"]) and  $_POST["something"] == "Peripherials")
    {   
        echo "Peripherials <br />";
    }

    if (isset($_POST["something"]) and  $_POST["something"] == "Processors")
    {   
        echo "Processors <br />";
    }

    ?>


    </body>
    </html>
<html>
<body>
<div id="main">
    <form action = "" method = 'POST'>
    <select name = "something">
    <option value = "Monitors">Monitors</option>
    <option value = "Graphics">Graphics</option>
    <option value = "Peripherials">Peripherials</option>
    <option value = "Processors">Processors</option>
    </select>
    <input type = "submit" value = "OK">
    </form>
</div>


<?php

// If you want to only one value then you can use below. it will echo selected option
if (isset($_POST["something"])) {

    echo $_POST["something"];
}


//you can check isset and compare post value.
if (isset($_POST["something"]) && $_POST["something"] == "Monitors")
{   
    echo "Monitors <br />";
}

if (isset($_POST["something"]) && $_POST["something"] == "Graphics")
{   
    echo "Graphics <br />";
}

if (isset($_POST["something"]) && $_POST["something"] == "Peripherials")
{   
    echo "Peripherials <br />";
}

if (isset($_POST["something"]) && $_POST["something"] == "Processors")
{   
    echo "Processors <br />";
}

?>


</body>
</html>

你可以使用这个:

if (isset($_POST["something"]) && $_POST["something"] == "Monitors")
{   
    echo "Monitors <br />";
}