Skip to content

Instantly share code, notes, and snippets.

@ccagle8
Last active December 19, 2015 14:48
Show Gist options
  • Save ccagle8/5971898 to your computer and use it in GitHub Desktop.
Save ccagle8/5971898 to your computer and use it in GitHub Desktop.
A simple way to display Twitter Bootstrap messages dynamically with PHP.
<?php
/**
* Display Alert Messages
*
* Looks for a global variable, and displays any test that variable has
*
* @uses GLOBALS $notice, $error, $success, $infonotice, $alertBlock
* @return string
*/
function display_messages() {
global $success;
global $error;
global $notice;
global $infonotice;
global $alertBlock;
if ($alertBlock) $alertBlock = ' alert-block ';
if ($notice) {
echo '<div class="alert '.$alertBlock.'"><button type="button" class="close" data-dismiss="alert">&times;</button>'. $notice .'</div>';
}
if ($error) {
echo '<div class="alert alert-error '.$alertBlock.'"><button type="button" class="close" data-dismiss="alert">&times;</button>'. $error .'</div>';
}
if ($success) {
echo '<div class="alert alert-success '.$alertBlock.'"><button type="button" class="close" data-dismiss="alert">&times;</button>'. $success .'</div>';
}
if ($infonotice) {
echo '<div class="alert alert-info '.$alertBlock.'"><button type="button" class="close" data-dismiss="alert">&times;</button>'. $infonotice .'</div>';
}
}
# test the messages
$error = 'Your payment has failed.';
$success = 'Your payment has succeeded!';
$notice = '<b>Tip:</b> When walking, you should have your eyes opened.';
$alertBlock = true;
$infonotice = '<h4>Welcome!</h4><p>Thanks for signing up. Check out this <a href="">great tutorial</a> to get you started.</p>';
?>
<!doctype html>
<html lang="en">
<head>
<title>Twitter Bootstrap Messages</title>
<!-- Dont forget to download and use your own copy -->
<link href="http://twitter.github.io/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<?php display_messages(); ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment