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.
Using FTP and PHP to get an array of file details such as size and…
Creating and using Laravel form requests to create cleaner code, separation and reusability for your…
Improving the default Laravel login and register views in such a simple manner but making…
Laravel validation for checking if a field value exists in the database. The validation rule…