Do not mix client logic with server logic. Exactly the same script can output the form and accept it. If the entry is successfully verified, it continues. If not, it will display the form again, this time with error messages and already entered data.
The next time the user submits the form, validation starts again until it passes successfully.
Thus, you expand the form with input values ββand error messages in the first place, but you only show them if they are marked / installed.
This can only be done with additional variables next to $_POST - or if you like it - using a complete abstraction of the form from the framework, such as the zend framework (which can be overhead for what you like) or just with the library / component, similar to the popular HTML_QuickForm2 .
Edit:
This is very simple code to demonstrate the general methodology, if you use the library, it is much nicer (and you do not need to code it, instead you can focus on the actual form, like the definition above). This code is more for reading and understanding the stream than for using, I quickly typed it so that it (certainly has) syntax errors, and it is not complete for a full-blown form. This has only one email field and there is even no submit button:
$request->isSubmit = isset($_POST['submit']); $form->fields = array ( 'email' => array ( 'validate' => function($value) {return filter_var($value, FILTER_VALIDATE_EMAIL);}, 'output' => function($value, $name) {return sprintf('<input type="text" value="%s" id="%s">', htmlspecialchars($value), htmlspecialchars($name)}, 'default' => ' info@example.com ', ), ); function get_form_post_data($form, $request) { $data = array(); foreach($form->fields as $name => $field) { $data[$name] = $field->default; if ($request->isSubmit && isset($_POST[$name])) { $data[$name] = $_POST[$name]; } } return $data; } function validate_form_data($form, $data) { foreach($form->fields as $name => $field) { $value = $data[$name]; $valid = $field['validate']($value); if (!$valid) { $form->errors[$name] = true; } } } function display_form($form, $data) { foreach($form->fields as $name => $field) { $value = isset($data[$name]) ? $data[$name] : ''; $hasError = isset($form->errors[$name]); $input = $field['output']($name, $value); $mask = '%s'; if ($hasError) { $mask = '<div class="error"><div class="message">Please Check:</div>%s</div>'; } printf($mask, $input); } }