When I have fields under a form
tag, I use isset()
function such as below:
if(isset($_POST) and isset($_POST['title']) and $_POST['date'])
{
//something;
}
and it gets bigger when I validate more fields. I am looking for any easy way of validate whether they are all filled or not in server-side with PHP.
Maybe something which navigates all the fields and check whether they are filled as required.
A more reusable solution would be to write a small function to verify a list of keys exist, such as
which could then be used for all your forms like this:
Frankly, I'm not sure why this isn't already a built-in function
$_POST
is an array, so you can fetch its element usingforeach
and then test each element if it is empty or no, collecting errors if there and at the end send one error message.I've found it amongst comment section on PHP Manual...
A little function which take an array as keys: