在收到输入之前显示回声

时间:2018-09-16 04:34:18

标签: php if-statement

我是PHP新手。基本上,我正在尝试做出一个简单的if else语句,该语句将使某人知道他们是否太年轻而无法观看额定R电影。我遇到的问题是,我想要输出的内容在获得输入(年龄)之前就已经显示出来了。

enter image description here

这是我当前的代码:

<form action="movieProgram.php" method="post">
How old are you? <input type="text" name="age"/>
<input type="submit">
</form>



<?php 

/*if ($age < 17){ 
The $_POST['age'] retrieves data from the
form that is named age

*/
$age = $_POST["age"];
if ($age == ""){
    echo "";
}
elseif ($age < 17){
    echo "You are too young to watch the movie.";
}
else {
    echo "You are old enough to watch the movie.";
}
 ?>

我该如何解决?有什么建议吗?

1 个答案:

答案 0 :(得分:3)

您可以在提交按钮上放置一个name属性,然后检查表单是否已提交。 这是我所谈论的演示

<form action="#" method="post">
How old are you? <input type="text" name="age"/>
<input type="submit" name="submit">
</form>



<?php 

/*if ($age < 17){ 
The $_POST['age'] retrieves data from the
form that is named age

*/
if(isset($_POST['submit'])) {
    $age = (int)$_POST["age"];
    if ($age == ""){
        echo "";
    }
    elseif ($age < 17){
        echo "You are too young to watch the movie.";
    }
    else {
        echo "You are old enough to watch the movie.";
    }
}

 ?>