PHP - 调用自身的表单操作,如何在 1 页中显示所有内容?

标签 php html forms

我有两个 .php 文件:

test1.php

<html>
<head>
    <title>Sample</title>
</head>
<body>
    <form action="test2.php" method="post">
        Please enter a number <input type="number" name="userNumber"><br>
        <input type="submit">
    </form>
</body>
</html>

test2.php

<html>
<head>
    <title>Sample Display Page</title>
</head>
<body>
<?php   
    $user_number = $_POST["userNumber"];
    echo "You have chosen $user_number";    
?>
</body>
</html>

我想知道如何才能将所有内容显示在一个页面上,即在 test1.php 上,而不必有两个文件。

最佳答案

检查是否 $_POST["userNumber"] isset , 如果不是,则回显表单。

<html>
<head>
    <title>Sample</title>
</head>
<body>
    <?php
        if(isset($_POST["userNumber"]){
            echo "You have chosen ".$_POST["userNumber"];
        }else{
            echo '<form action="test1.php" method="post">';
            echo 'Please enter a number <input type="number" name="userNumber"><br>';
            echo '<input type="submit">';
            echo '</form>';
        }
    ?>
</body>
</html>

关于PHP - 调用自身的表单操作,如何在 1 页中显示所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22209036/

相关文章:

python - 获取 Selenium Python 中 Html 按钮生成的内容

html - 在浏览器中与在缓冲区中使用 Selenium 进行 headless 测试

html - HTML+CSS 中的时间线示例,如 Facebook 的时间线

html - 输入提交按钮的图像背景和文本 - CSS

php - 如何在wordpress页脚中包含php并在页面上调用

php - artisan 迁移——种子错误

php - 在事件记录 codeigniter 中结合 get_where 和 limit

php - 在 CodeIgniter 中 key 未知时获取发布值

javascript - 当 Angular 指令的名称真的很重要时

java - JNI : Connecting Java with modules in other programming languages