How to deal with errors, warnings, alerts or notices with PHP. You cannot hard code them into a page because at times they wont be needed yet when they are how can they be shown in a simplistic manner?
Lets assume you submit a form and a field was filled in wrong or it had missing data, you can return the user to the page or another page but this time show an alert styled response on the issue/error, here is how.
A function that sorts and returns based on the int
function alert_system($id = 4)
{
switch ($id) {
case 1:
return "
This is a notice that ......
";
break;
case 2:
return "
This is a warning that ......
";
break;
case 3:
return "
This is an error notice that ......
";
break;
default:
return "";
}
} 1, 2 or 3 will return the alert html whilst 4 or higher returns nothing i.e no issue.
On our form submit page filter to the error and do a redirect with a GET like:
if (!is_numeric($age)){
header("Location: somepage.php?notice=3");
die();
} Then the “somepage.php” has this code included (along with Bootstrap and the require_once(‘functions.php’);)
Page heading here
Content
Content
It will display the alert notice pending what the issue is. If the ?notice= isn’t set then nothing gets shown as per the function.
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Some early September spring shots from a bushwalk, It is still too early for wildflowers.
I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…
If you ever needed a simple solution in Laravel to delete an entire directory and…
How to enforce only one user to register or be registered in a Laravel web…