Development

Easy alert system with PHP

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 "";
            break;
        case 2:
            return "";
            break;
        case 3:
            return "";
            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.

 

Share
Tags: PHPWeb Dev

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago