I am working on a PHP project and I have a file called login.php. This file contains a HTML code that basically consists of a form that has 2 inputs (used for email and password) and a submit button. Before the HTML there is a PHP script that checks if the inputs are valid (not empty, the user exists in the db). I use an hidden input field (called formsubmitted) to check if the user has pressed the submit button. If the values are valid then the PHP script redirects the user to the home page, if not, the PHP script prints a HTML div in which I store the error message and then the HTML for the form.
I would like to print only the error div, not the rest of the HTML, and for this I could use the exit() function. Now I don't know if this is a good practice, I have searched the web but didn't find any useful. I would appreciate if someone could tell me if it is ok to do so. Thanks!
Here is how my code looks briefly:
<?php
if (isset($_POST['formsubmitted'])) {
//check if the email and password are valid
if (valid) {
header("Location:home.php");
} else {
echo "<div id='error-section'>";
echo "message";
echo "</div>"
}
}
exit();
?>
<!DOCTYPE html>
<html>
...............
</html>