在模态php中显示表单错误消息

时间:2020-08-18 17:49:20

标签: php forms error-handling bootstrap-modal session-variables

我有几条错误消息要显示,在检查表单时,我想以引导方式显示它们。

我当时正在考虑使用会话,但是我不知道出现错误时如何触发模式。我应该使用js还是jQuery?如果您有更好的方式来美观地处理错误消息,请提前...

以下是我的代码的摘录:

PHP部分

// Clean retrieved client data
$client = dataCleaning($_POST['client']);
// Check if the client name is at least 2 characters
if(mb_strlen($client) < 2 ){
    // echo'Le nom du client doit comporter au moins 2 caractères';
    $_SESSION['error'] = 'Le fichier doit être une image png ou jpg';
    header('Location: test.php');
    die;
}

HTML部分

if(isset($errors) && !empty($errors)):?>
<div class="modal fade" style="background-color: #e64827" id="errorModal" tabindex="-1" role="dialog" data-backdrop="static" data-show="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title font-weight-bold text-uppercase" id="errorModalLabel">
                     <i class="fas fa-exclamation-circle" style="color: #e64827"></i>
                        Erreur
                </h5>
                    
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
             </div>
             <?php foreach($errors as $error):?>
                  <div class="modal-body font-weight-bold" id="error_message">
                        <?=$error;?>
             <?php endforeach;?>
             <div class="modal-footer">
                  <a href="<?= $_SERVER['HTTP_REFERER'] ?>"><button type="button" class="btn btn-secondary">Retour</button></a>
             </div>
         </div>
     </div>
</div>
<?php endif;?>  

0 个答案:

没有答案
相关问题