SillySam
SillySam

Reputation: 98

PHP - Post alert at the top of the page

Is it possible to display an error sourced from PHP in a bootstrap alert at a certain part of HTML? Their are alerts within the example.php files which I would like to show on the main page. I tried creating a function in PHP were i wanted the alert to go like so...

Main File function

function postAlert($good, $text) {
    $type = 'danger';
    if($good){
        $type = 'success';
    }
    echo '<div class="alert alert-'.$type.' alert-dismissable"> <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong> '. $text.' </strong></div>';
  }

Example File that was included

...
postAlert(true,'This is an error example');
...

I tried calling this function in one of the example php files but nothing appeared to happen and no errors occurred. Is there something I'm doing wrong or is there a better way of doing this?

<?php include 'navbar.php'; ?>

<!-- Alert goes here -->

<div class="container">
  <div class="row">
    <?php include 'example1.php' ?>
    <?php include 'example2.php' ?>
    <?php include 'example3.php' ?>
  </div>
</div>

Upvotes: 0

Views: 196

Answers (1)

B. Desai
B. Desai

Reputation: 16436

In php . is used for concatenation.While you are using + in alert. change your function as below

function postAlert($good, $text) {
    $type = 'danger';
    if($good){
        $type = 'success';
    }
    echo '<div class="alert alert-'.$type.' alert-dismissable"> <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong> ' . $text . ' </strong></div>';
  }

Upvotes: 1

Related Questions