结合isset和elseif语句


Combining isset and elseif statements

我想问一下这段代码是否可行?我想发生的是,只有在按下提交按钮后,它才会回应。

<!DOCTYPE html>
<html>
<body>
<?php
$x = $_POST['x'];
$y = $_POST['y'];
$z = $x + $y;
if (isset($_POST['submit'])) {
    if($z < "10") {
        echo "Higher!";
    } elseif ($z > "10"){
        echo "Lower!";
    } else {
        echo "You're right!";
    }
}
?>
<form action="index.php" method="post">
<input type="number" name="x">
&nbsp;+&nbsp;
<input type="number" name="y">
<input type="submit" value="EQUALS">
</form>
</body>
</html>

您必须将某个字段命名为"submit",因为在if语句中,您要检查是否设置了名称为submit的字段。您的HTML中没有这样的字段。您可以使用提交按钮。您唯一需要做的就是将值为submit的属性name放在submit按钮中。

<!DOCTYPE html>
<html>
    <body>
        <?php
        if (isset($_POST['submit'])) {
            $x = $_POST['x'];
            $y = $_POST['y'];
            $z = $x + $y;
            if($z < 10) {
                echo "Higher!";
            } elseif ($z > 10){
                echo "Lower!";
            } else {
                echo "You're right!";
            }
        }
        ?>
        <form action="" method="post">
            <input type="number" name="x">
            &nbsp;+&nbsp;
            <input type="number" name="y">
            <input type="submit" name="submit" value="EQUALS">
        </form>
    </body>
</html>