I have a form
that user can update their details on page-1.php
.
After user submit the form
, it will got to page-2.php
for validation and update database if user input is true.
May i know how to call a function from page-2.php
and display the error message on page-1.php
. Below are the function on page-2.php
that I use to display the error message:
$_SESSION['err_msg'] = array();
function addError($msg) {
$_SESSION['err_msg'][] = $msg;
}
function printErrors() {
foreach ($_SESSION['err_msg'] as $err){
echo "<ul><li><span style='color: red;'>".$err."</span></li></ul>";
}
unset($_SESSION['err_msg']);
}
//other codes for the validation and update
if i use printErrors();
on page-2.php
, it will display the error message
You need to include page-2.php in page-1.php. Read this about that.
The best way is to put all your functions in one seperate file, like functions.php and include them on every page with an include.
I think no you don't need function on page-2.php. You can simply check session variable on page-1.php. If $_SESSION['err_msg'] exists and count is greater than 0. Then you can print all errors to page-1.php> Please check below code.
if(isset($_SESSION['err_msg']) && count($_SESSION['err_msg'])>0 ){
foreach ($_SESSION['err_msg'] as $err){
echo "<ul><li><span style='color: red;'>".$err."</span></li> </ul>";
}
unset($_SESSION['err_msg']);
}
Hope this helps you.
Include the page-2.php in your page-1.php