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