Call Bootstrap Alert with PHP Function

15 Jan

PHP Bootstrap Alert in a Function

Here is a simple way I call Bootstrap Alerts using a PHP Function.

Here is how you normally create an alert using Bootstrap

Bootstrap Alert

<div class="alert alert-success" role="alert">
  <p>Hello World!</p>
</div>

Below is the PHP code that uses a function to generate the Bootstrap Alert

PHP Code

<?php
function displayAlert($text, $type) {

echo "<div class=\"alert alert-".$type."\" role=\"alert\">
        <p>".$text."</p>
      </div>";
}
?>

Now whenever you want to display the alert you simply call the function.

You can change the style of the alert box with the second parameter.

  • success
  • info
  • warning
  • danger

Example of different alert styles

bootstrap-alert-style

Call PHP function to display Bootstrap alert with customized message

<?php
displayAlert("Hello World!", "success");
?>

 

Sample Output

alert-success

This is a simple way to generate Bootstrap Alert boxes when you need them without having to type out the code for the alert box.

Hope this helps making your coding easier!

-Tony

Leave a Reply

Your email address will not be published. Required fields are marked *