-1
<!DOCTYPE HTML>  
<html>
<head>
<style>
.error {color: #eb0800;}
</style>
</head>
<body>   

<?php

$firstname = ["firstname"];
$surname = ["surname"];
$username= ["username"];
$password =  ["password"];
$firstnameerror = $surnameerror = $usernameerror = $passworderror = "";

$firstnameerror = "Please enter your First Name";
$surnameerror = "Please enter your Surname";
$usernameerror= "Please enter your Username";
$passworderror= "Please enter your Password";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["firstname"])) {
    $firstnameerror = "First name is required";
  } else {
    $firstname = ($_POST["firstname"]);
    
    if (!preg_match("/^[a-zA-Z-' ]*$/",$firstname)) {

    }else{
        $firstnameerror = "You have entered the First Name correctly";
    }
  }
}

    if(empty($_POST["surname"])){
        $surnameerror = " Surname must be entered";
    } else{
        $surname = ($_POST["surname"]);
        if (!preg_match("/^[a-zA-Z-' ]*$/",$surname)) {

    }else{
        $surnameerror = "You have entered the Surname correctly" ;
    }
  }
    
        
    
    if(empty($_POST["username"])){
        $usernameerror = "Username must be entered";
    }else{
        $username = ($_POST["username"]);
        if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
      $usernameerror = "Invalid Username try Again";
    }else{
        $usernameerror = "You have entered the email correctly";
    }
    }
    if(empty($_POST["password"])){
        $passworderror = "Password must be entered";
    }else{
        $password = ($_POST["password"]);
    }
    
        

function ($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>


<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  First Name: <input type="text" name="firstname">
  <span class="error">* <?php echo $firstnameerror;?></span>
  <br> <br>
   Surname: <input type="text" name="surname">
  <span class="error">* <?php echo $surnameerror;?></span>
  <br><br>
  Username: <input type="text" name="username">
  <span class="error">* <?php echo $usernameerror;?></span>
  <br><br>
  Password: 
  <input type="password" minlength="5" required>
  <span class="error">* <?php echo $passworderror;?></span>
  <br> <br>
  <input type="submit" name="register" value="submit">  
</form>



</body>
</html>

when I run this code it works fine however I would like the input fields to say please enter your first name for the rest below and not the first name is required as I want that to happen after there are no fields in and they click submit I'm very new to this so sorry.Also I do not see the error and am having lots of trouble with this can someone explain to me why.

  • Have you seen this post : https://stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message It seems to be the same issue. – cecile Mar 31 '21 at 11:27
  • 3
    Sorry can you re-write your description? It reads like one big stream of consciousness instead of a set of proper sentences. This is not a telephone call - please write clearly and list each thing you want to happen separately. Use bullets and formatting as well, where appropriate. Thanks. – ADyson Mar 31 '21 at 11:27
  • 2
    P.S. Possibly this is what you need: https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation - but I can barely make sense of your request to be honest. Please take a deep breath and re-write your question in a way that is understandable to others. – ADyson Mar 31 '21 at 11:28
  • If you want to have any content in your form fields, why not put it there? – Nico Haase Mar 31 '21 at 11:37

1 Answers1

0

Add placeholder="Please enter your first name" to have grayed out text that disappears when the user types in something.

Also, you can add required="required" to the input to prevent the user from submitting the form if a field is empty (if I understood the question right it's what you also wanted?)

Your input for first name will then look like this:

<input type="text" name="firstname" required="required" placeholder="Please enter your first name">