PHP - form action calling itself, how to display everything in 1 page?

后端 未结 9 1347
暖寄归人
暖寄归人 2021-01-22 14:43

I have two .php files as such:

test1.php



    Sample


    
相关标签:
9条回答
  • 2021-01-22 15:14

    Sample

    <form action="<?php $_PHP_SELF ?>" method="post">
        Please enter a number <input type="number" name="userNumber"><br>
        <input type="submit">
    </form>
     </body> </html>
    
    0 讨论(0)
  • 2021-01-22 15:17
    <html>
    <head>
    <title>Sample</title>
    </head>
    
    <body>
    <?php   
        if (isset($_POST['submitted'])) { 
        $user_number = $_POST["userNumber"];
        echo "You have chosen $user_number";    
        }
    else {
    ?>
    <form method="post">
        Please enter a number <input type="number" name="userNumber"><br>
        <input type="submit" name="submitted">
    </form>
    <?php
    }
    ?>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-22 15:17

    set the variables up before the form as placeholders and check for isset

    <?php
    $user_number = '';
    if(isset($_POST["userNumber"])) { 
        $user_number = $_POST["userNumber"];
    }
    ?>
    
    <form action="test2.php" method="post">
        Please enter a number <input type="number" name="userNumber"><br>
        <input type="submit" name="submit">
    </form>
    
    <p>You have chosen: <?php echo $user_number ?></p>
    
    0 讨论(0)
  • 2021-01-22 15:22

    Just use isset() for submit button, note that i added name="submit" to your button since it was missing

    <html>
    <head>
        <title>Sample</title>
    </head>
    <body>
    <?php   
    if(isset($_POST['submit'])
    {
        $user_number = $_POST["userNumber"];
        echo "You have chosen $user_number";    
    } else {
    ?>
        <form action="test2.php" method="post">
            Please enter a number <input type="number" name="userNumber"><br>
            <input type="submit" name="submit">
        </form>
    <?php } ?>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-22 15:28

    Pretty simple actually:

       <html>
        <head>
            <title>Sample</title>
        </head>
        <body>
            <?php   
            if(isset($_POST) && isset($_POST['userNumber'])) {
                $user_number = $_POST["userNumber"];
                echo "You have chosen $user_number";    
            }
            ?>
            <form method="post">
                Please enter a number <input type="number" name="userNumber"><br>
                <input type="submit">
            </form>
        </body>
        </html>
    
    0 讨论(0)
  • 2021-01-22 15:31

    Your test1.php will need to look like this

    <html>
    <head>
        <title>Sample</title>
    </head>
    <body>
        <form action="test1.php" method="post">
            Please enter a number <input type="number" name="userNumber"><br>
            <input type="submit">
        </form>
        <?php
        if(isset($_POST["userNumber"])) { 
            $user_number = $_POST["userNumber"];
            echo "You have chosen $user_number";
        }
        ?>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题