简体   繁体   English

PHP-HTML-即使提交后仍显示输入表单

[英]PHP - HTML - Displaying input form even after submit

I have two input forms and would like the second one to stay on the page even when it is submitted. 我有两个输入表单,并且即使提交后也希望第二个保留在页面上。

<div id="first">
    <form method="POST">
        Number: <input type="text" name="number"><br>
        <input type="submit" value="Submit">
    </form><br>
</div>

<div id="second">
    <?php
    if (isset($_POST['number']) && !empty($_POST['number'])){
        ?>
        <form method="POST">
            Name: <input type="text" name="name"><br>
            <input type="submit" value="Submit">
        </form><br>
        <?php
    }
    ?>
</div>

<div id="third">
    <?php
    if (isset($_POST['name']) && !empty($_POST['name'])){
        echo "TEST";
    }
    ?>
</div>

When I submit my first form, the second form appears correctly since $_POST['number'] is not empty. 当我提交第一份表格时,第二张表格正确显示,因为$ _POST ['number']不为空。 However, the content of 'number' disappears as soon as I submit it. 但是,“数字”的内容在我提交后就消失了。

Then, when I submit the second form, the word "TEST" appears correctly but the form itself disappears since $_POST['number'] from the first form is now empty. 然后,当我提交第二个表单时,单词“ TEST”正确显示,但是表单本身消失了,因为第一个表单中的$ _POST ['number']现在为空。

I need to find a way to somehow save the value of number in the first form so that the second form does not disappear. 我需要找到一种方法以某种方式将number的值保存在第一种形式中,以使第二种形式不会消失。

Any suggestions? 有什么建议么?

You can add hidden field: 您可以添加隐藏字段:

<input type="hidden" name="number" value="<?php echo $_POST['number']; ?>">

Then your second form will be changed to: 然后,您的第二种形式将更改为:

<form method="POST">
    Name: <input type="text" name="name"><br>
    <input type="hidden" name="number" value="<?php echo $_POST['number']; ?>">
    <input type="submit" value="Submit">
</form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM