-1

Hello all and thanks in advance for the help,

I am struggling to understand why the PHP code I have is not looping through each input field and posting/getting the output.

<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>

This is the form I am trying to get to work

   <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>
Koala Yeung
  • 7,475
  • 3
  • 30
  • 50

2 Answers2

0

The names of html input elements in a form are used as “key” in http get / post array on form submit.

Your input elements do not have any name property. So they are not submitted properly. Add names to inputs. For example

<input type="password" name=“password” placeholder="Password" class="form__input" required>
smartdroid
  • 312
  • 2
  • 10
0

You have forgot to put name attribute in your form input items, like this:

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

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

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

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

Without the name attribute, the browser won't know what key to use for each fields.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50