Answer the question
In order to leave comments, you need to log in
How to properly organize the storage of errors in the session?
Good afternoon. When processing forms on different pages, errors are accumulated in the $errors array. Also, upon successful registration, etc., messages are accumulated in the $successMessage array.
The question is how to correctly save errors to the session for subsequent output on the same page or on others when redirecting. Now I did it like this:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$errors[] = "Заполните имя!";
}
if (empty($_POST["age"])) {
$errors[] = "Заполните возраст!";
}
$_SESSION["errors_forms"] = $errors;
if (count($errors) === 0) {
$successMessage[] = "Форма успешно отправленна!";
$_SESSION["success_forms"] = $successMessage;
header("Location: obr_form.php");
exit();
}
}
Answer the question
In order to leave comments, you need to log in
$errs = [];
if(isset($_SESSION['errors_form'])){
$errs = $_SESSION['errors_form'];
unset($_SESSION['errors_form']);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question