首页 > 解决方案 > 表单 POST 提交未按预期生成 $_POST 键值

问题描述

大家好,提前感谢您的帮助,

我很难理解为什么我拥有的 PHP 代码没有遍历每个输入字段并发布/获取输出。

<html>

<body>
    <!-- This PHP code will loop through all of the input fields and output the IDs and names. Both options of the get and post methods are checked. -->
    <?php

    foreach ($_POST as $key => $value) {
        echo $key . ": " . $value . "<br/>";
    }
    foreach ($_GET as $key => $value) {
        echo $key . ": " . $value . "<br/>";
    }

    ?>
</body>

</html>

这是我想要开始工作的表格

   <form class="form" action="response_page.php" method="post">
            <div class="form__group">
                <input type="text" placeholder="Username" class="form__input" required>
            </div>

            <div class="form__group">
                <input type="email" placeholder="Email" class="form__input" required>
            </div>

            <div class="form__group">
                <input type="password" placeholder="Password" class="form__input" required>
            </div>

            <input type="submit" name="submit" value="Submit" class="btn">
        </form>

标签: php

解决方案


表单中 html 输入元素的名称在表单提交时用作 http get/post 数组中的“键”。

您的输入元素没有任何名称属性。所以他们没有正确提交。为输入添加名称。例如

<input type="password" name=“password” placeholder="Password" class="form__input" required>

推荐阅读