I am fairly new to server-side validation and I currently post my form values to the page below.
<form id="sign_in" action="<?php echo htmlspecialchars('inc/validate.php') ?>" method="POST">
I perform other operations on the page so I'd like all the code to be in one place. I would like to handle all of my validation on this page and send back the error messages to the form page. However, alot of the examples I'm seeing are using:
action="<?php echo $_SERVER['PHP_SELF']; ?>"
validate.php has the following code
<?php
include('prc_input_validation.php');
// check if username and password isset and are not empty
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Retrieve values from login form
$username = checkInput($_POST['username']);
$password = checkInput($_POST['password']);
$userNameempty = emptyCheck($username);
if($userNameempty == "true")
{
$empty_error = "This field is required";
header("location: ../sign-in.php");
}
}
?>
prc_input_validation.php has the following code
<?php
function checkInput($inputField) {
$inputField = trim($inputField); // Strip whitespace (or other characters) from the beginning and end of a string
$inputField = stripslashes($inputField); //Un-quotes a quoted string
$inputField = htmlspecialchars($inputField); //Convert special characters to HTML entities
return $inputField;
}
function emptyCheck($inputField)
{
if(empty($inputField))
{
return "true";
}
else
{
return "false";
}
}
?>
Form input
<input type="email" class="form-control" name="username" placeholder="Username" autofocus required>
<span class="text-danger"><?php echo $empty_error; ?></span>
How can I correctly send back the error message to the form for it to be displayed? on the form