Categories: 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…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago