You can have PHP add a class to your inputs if a field is invalid, but you may need to adjust your approach.
If you want to maintain using a form POST method (as you have it), you'd want to move your validation code to be processed on the same page that renders your view (the easiest way to do this is to have it within the same page). You can still use and include a separate file to pull in a PHP class or other system you want to use for validation if you want to keep your code neat. Here's an example:
// filter_input returns null or false if invalid.
$firstname = filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_STRING);
?>
However, I would recommend a more robust approach which would be to use an AJAX handler for submitting your form. This will give your users a better experience, and it also allows you to neatly have your form processing entirely separate from your view. That solution is a bit beyond the scope of this question, but ultimately you would have your PHP AJAX handler return information as to whether or not the submission was valid or what fields are invalid and have the javascript add your invalid class to the fields in error.