skip to Main Content

I’m trying to make a concept sign up page in PHP, and I need to require the user to enter in text for ALL inputs. If not, it would say for example, "You need a valid email address!" or something like that.

My problem is getting the text to actually show. When I submit, it doesn’t show the error, it rather just stays with a star.

My code:

<html>
  <head>
    <link rel="stylesheet" href="style.css">
    <title>Test</title>
  </head>
  <body>
    <?php
      $emailErr = $usernameErr = $passwordErr = "";
      $email = $username = $password = "";

      if ($_SERVER["REQUIRED_METHOD"] == "POST") {
        if (empty($_POST["email"])) {
          $emailErr = "You must enter a valid email address!";
        } else {
          $email = testData($_POST["email"]);
        }
        if (empty($_POST["username"])) {
          $usernameErr = "You must create a username!";
        } else {
          $username = testData($_POST["username"]);
        }
        if (empty($_POST["password"])) {
          $passwordErr = "You must create a password!";
        } else {
          $password = testData($_POST["password"]);
        }
      }
      function testData($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
      }
    ?>
    <p><span class="error">* Required Field(s)</span></p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      <label for="email">Email Address:</label>
      <span class="error">* <?php echo $emailErr;?></span>
      <br>
      <input type="email" name="email" placeholder="[email protected]" />
      <br><br>
      <label for="username">Username:</label>
      <span class="error">* <?php echo $usernameErr;?></span>
      <br>
      <input type="text" name="username" />
      <br><br>
      <label for="password">Password:</label>
      <span class="error">* <?php echo $passwordErr;?></span>
      <br>
      <input type="password" name="password" />
      <br><br>
      <input type="submit" value="Create Account!">
    </form>
  </body>
</html>

Can anyone please help me find out a solution to my situation?

3

Answers


  1. It is not $_SERVER["REQUIRED_METHOD"] it should be $_SERVER["REQUEST_METHOD"]
    So your if conditional block will be

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["email"])) {
            $emailErr = "You must enter a valid email address!";
        } else {
            $email = testData($_POST["email"]);
        }
        if (empty($_POST["username"])) {
            $usernameErr = "You must create a username!";
        } else {
            $username = testData($_POST["username"]);
        }
        if (empty($_POST["password"])) {
            $passwordErr = "You must create a password!";
        } else {
            $password = testData($_POST["password"]);
        }
    }
    

    Also, another quick and easy way of debugging is using var_dump() and see if the code block is being executed and has desired data in the variables is set.

    You could have done var_dump($_SERVER); too to quick check what data this super global variable has or inside the if block so you will be sure that truth condition did passed or not.

    Login or Signup to reply.
  2. $_SERVER["REQUIRED_METHOD"] == "POST" will raise an error.
    $_SERVER["REQUEST_METHOD"] == "POST" will be right answer for your question.

    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    
    Login or Signup to reply.
  3. Change index of $_SERVER["REQUIRED_METHOD"] to REQUEST_METHOD.

    For more details go to https://www.php.net/manual/en/reserved.variables.server.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search