Why this form submit isn't working as expected

2019-09-10 05:11发布

I have this code in test.php.

  • When I access the script just by typing it, I want to see just the form
  • but when I click the form submit button, the next time the page loads, I want to see the form and some comment that says that the form has been submitted.

For some reason, even when I click submit, I don't get the message that it's posting. Anyone can explain why? and how can I get it to work.

<body>
    <form action="" method="post">
        <input type="text" id="inp" />
        <input type="submit" value="submit" />      
    </form>

    <?php
    if (isset($_POST['submit'])) {
        echo "posting";
    }

    ?>
</body>

3条回答
闹够了就滚
2楼-- · 2019-09-10 05:56

Give a name to the input:

<input type="submit" name="submit" value="submit" /> 
查看更多
一纸荒年 Trace。
3楼-- · 2019-09-10 06:03
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "posting";
}
查看更多
Rolldiameter
4楼-- · 2019-09-10 06:07

Try this:

<body>
    <form action="" method="post">
        <input type="text" id="inp" />
<input type='hidden' name='submit' value=''>
        <input type="submit" value="submit" />      
    </form>

    <?php
    if (isset($_POST['submit'])) {
        echo "posting";
    }

    ?>
</body>
查看更多
登录 后发表回答