(php) flash-message plugin

For a long time I’ve wanted a way to send messages from my controller to my view, when posting data. The message could be “Your update went well” or some error.

In the old days, I have done it like this:

controller, users/update.php:

<?php
$result = mysql_query("update users set email = '".$_POST['email']."' where id = ".$_POST['id']);

if($result) {
  header("Location: /users/index.php?message='Some long message, that might bug out'");
}
else {
  header("Location: /users/edit.php?message='ERROR because....'");
}

?>

view, users/index.php and users/edit.php:

<?php
echo $_GET['message'];
?>

However, these days are over now. With my little plugin, you can easily pass messages from the controller to the view.
All you need to do, is to include the script on your pages, then call: flash(label, message) from your controller. From your view, you can call echo getFlash(label) and that is it!

Using the same example from before, we get the following:

controller, users/update.php:

<?php
$result = mysql_query("update users set email = '".$_POST['email']."'  where id = ".$_POST['id']);

if($result) {
  // set the message
  flash('success', 'Some long message, that will NOT bug out');
  // do the redirect
  header("Location: /users/index.php");
}
else {
  // set the message
  flash('error', 'You had an error in your data.');
  // do the redirect
  header("Location: /users/edit.php");
}
?>

view, users/index.php:

<?php

echo getFlash('success');

?>

view, users/edit.php:

<?php

echo getFlash('error');

?>

Head on over to the github reposition at get it: http://github.com/jimmiw/php-flash-message

I hope you enjoy it