-1

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="someone@organization.com" />
      <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?

  • 2
    Stop using `testData()`, everywhere, everytime, it is wrong. Use proper methods for the place the data is being used/sent. `htmlspecialchars` on output, prepared statements and binding for SQL. There isn't a one shot, fix all security issues function. – user3783243 Feb 01 '23 at 04:53
  • You're obviously not seeing errors reported, otherwise you would have been alerted to the `REQUIRED_METHOD` problem. See the link at the top of your post for details on how to start seeing debugging information – Phil Feb 01 '23 at 05:32

3 Answers3

2

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.

Anuj Shrestha
  • 966
  • 6
  • 18
-1

Change index of $_SERVER["REQUIRED_METHOD"] to REQUEST_METHOD.

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

Saket B
  • 41
  • 1
  • 6
-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')
user3783243
  • 5,368
  • 5
  • 22
  • 41