Skip to content
Advertisement

Setting General Messages Using Flash Message

I have a simple register form, my form validates but will not show error messages or validation messages

This is my form function

function validate_new_user()
{
    $errors = [];

    if (isset($_POST['register'])) {

        $email = $_POST['email'];
        $name = str_replace(" ", "", $_POST['username']);
        $password = $_POST['password'];

        if (empty($email)) {
            $errors[] = "Email Address is required";
        }
        if (empty($name)) {
            $errors[] = "Username is required";
        }
        if (strlen($password) < 5) {
            $errors[] = "Password must be at least 6 characters long";
        }
        if (!empty($errors)) {
            set_message($errors[0], WARNING);
        } else if (create_new_user($email, $name, $password)) {

            set_message('Please check your email for user Information.', SUCCESS);
            redirect_to_url("/user/login");

        }
    }
 }

I call my validation function in my form page

<?php validate_new_user(); ?>

so if there is an error it should set message but don’t.

now if it successfully it redirects to login and sets a flash message also and I call it with

<?php display_message(); ?>

That don’t display a message either

Flash message code

define('SUCCESS', 'success');
define('INFO', 'info');
define('WARNING', 'warning');


function set_message($message, $type = 'success')
{
    if (!empty($_SESSION['flash_notifications'])) {
        $_SESSION['flash_notifications'] = [];
    }
    $_SESSION['flash_notifications'][] =
        $message = [
            '<div class="alert . $type .">$message</div>'
        ];
}


function display_message()
{
    if (isset($_SESSION['flash_notifications'])){
        return $_SESSION['flash_notifications'];
    }
}

my goal is to use one set message for all notifications with styles but I cannot get none of the messages to display

Advertisement

Answer

I’ll assume you’re calling session_start() at the beginning of the script.

Your usage of functions makes the problem much easier to diagnose! Sometimes, though, it helps to have a different set of eyes look at it.

Your function set_message() has a couple of errors:

  1. The initialization of $_SESSION['flash_notifications'] should occur if it is empty, but instead you are initializing if it is not empty. Hence nothing can be added
  2. Malformed assignment. When you are building the message array to save in $_SESSION, there is no need to reassign $message. Also, usage of single quotes does not interpret variables within the quotes, so the html snippet is not what you expect.

Corrected function:

function set_message($message, $type = 'success')
{
    if (empty($_SESSION['flash_notifications'])) {
        $_SESSION['flash_notifications'] = [];
    }
    $_SESSION['flash_notifications'][] = '<div class="alert '. $type .'">'.$message.'</div>';
}

Note, it might be more understandable to write it this way:

    $_SESSION['flash_notifications'][] = <<<FLASH

<div class="alert $type'">$message</div>
FLASH;

Your function display_message() is almost correct as is, except you’re returning an array, not a string. If you’re going to print it, it must be converted into a string:

function display_message()
{
    if (isset($_SESSION['flash_notifications'])){
        return join('',$_SESSION['flash_notifications']);
    }
}

Then when you call it in your html, use the short print tag instead of the regular <?php tag:

<!— somewhere in your view (html output) —>

<?= display_message() ?>

<!— continue html —>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement