我在会话变量中存储了一条错误消息,当条件变为false时,它会在页面上显示。现在,我希望在刷新页面时消息消失,从而释放会话变量。
代码: 显示错误消息
<?php
if (isset($_SESSION['er'])): //Showing Errors on Salary form ?>
<div class="form-errors">
<?php foreach ($_SESSION['er'] as $mistake): ?>
<p> <?php echo $mistake ?> </p>
<?php endforeach ;?>
</div>
<?php endif; ?>
存储错误
$_SESSION['er'] = array("Salary Must be between 10000 and 80000.");
header("Location:addSalary.php");
答案 0 :(得分:1)
打印错误后立即取消设置。因此它只会显示一次
<?php if (isset($_SESSION['er'])): //Showing Errors on Salary form ?>
<div class="form-errors">
<?php foreach ($_SESSION['er'] as $mistake): ?>
<p> <?php echo $mistake ?> </p>
<?php endforeach ;?>
</div>
<?php unset($_SESSION['er']); ?>
<?php endif; ?>
答案 1 :(得分:1)
然后,一旦您使用/发送了错误消息,就可以将其删除。
<?php
if (isset($_SESSION['er'])): ?>
echo '<div class="form-errors">';
foreach ($_SESSION['er'] as $mistake):
echo "<p>$mistake</p>";
endforeach;
echo '</div>';
unset($_SESSION['er']);
endif;
?>