2014-09-28 143 views
-1

通过PHP变量传入警报div的内容,但即使PHP变量为空,div也是可见的。当没有内容时隐藏“alert”div

<span class="alert alert-danger"><?php echo $sentFailed ?></span> 
<span class="alert alert-success"><?php echo $sentSuccess ?></span> 

现在它看起来像:enter image description here

我怎样才能让只有当有东西在集PHP变量的div可见。

我曾尝试:

<span class="alert alert-danger"><?php if(!empty($sentFailed)){echo $sentFailed;} ?></span> 
<span class="alert alert-success"><?php if(!empty($sentSuccess)){echo $sentSuccess;} ?></span> 

我找的引导,jQuery的或php的解决方案为主。

回答

2

使用

<?php 
if(!empty($sentFailed)){  
?> 
<span class="alert alert-danger"><?php echo $sentFailed; ?></span> 
<?php 
} 
?> 

这意味着如果PHP变量没有值,那么不要显示HTML标记,变量值,

否则同时显示。