如何在 PHP 联系表单的每个不同字段中显示错误消息?

我正在遵循教程并填写了此联系表。它工作正常,但我想在每个字段中显示不同的消息,而不是使用一个框。我试着移动

<?php if($msg != ''): ?>
            <div class="alert <?php echo $msgClass; ?>"><?php echo $msg;?></div>
         <?php endif; ?>

在输入字段下方,就在该位置显示而言效果很好,但它只是同时显示相同的消息。它不会单独显示不同的消息。从这里我将如何处理这个问题?

body {

    font-size:10px;

    font-family:sans-serif, "Open-sans";

    margin:0;

    padding:0;

    box-sizing:border-box;

    letter-spacing:0.1rem;

}


.navbar {

    background:#333;

    width:100%;

}


.container {

    max-width:1100px;

    margin:auto;

}


.navbar-header {

    display: flex;

    flex-flow: row wrap;

    justify-content: flex-start;

}


.navbar-brand {

    font-size:1.5rem;

    padding:1rem;

    color:#fff;

    text-decoration:none;

}


form {

    font-size:1.3rem;

}


.form-group {

    display:flex;

    flex-direction: column;

    margin:1.5rem;

}


label {

    color:#333;

    margin-bottom:0.7rem;

}


input, textarea {

    max-width:100%;

    border:0.5px solid darkslategray;

    padding:1.3rem;

    font-size:1.5rem;

}


button {

    background:rgb(67, 130, 211);

    color:#fff;

    font-size:1.2rem;

    padding:1rem;

    margin:1.5rem;

    border:none;  

}


.alert {

    margin:1.5rem;

    padding:1.5rem;

    font-size:1.5rem;

    color:#fff; 

}


.alert-danger {

    background-color:rgb(219, 54, 48);

}


.alert-success {

    background-color:rgb(28, 160, 39);

}

<?php 

    // Message Vars

    $msg = '';

    $msgClass = '';

    

    // Check for submit

    if(filter_has_var(INPUT_POST,'submit')){

        // Get Form Data 

        $name = htmlspecialchars($_POST['name']); 

        $email = htmlspecialchars($_POST['email']);

        $message = htmlspecialchars($_POST['message']);


        // Check Required Fields

        if(!empty($email) && !empty($name) && !empty($message)){

            // Passed

            // Check Email 

            if(filter_var($email, FILTER_VALIDATE_EMAIL) === false ){

                // Failed

                $msg = 'Please use a valid email';

                $msgClass = 'alert-danger';

            } 

慕尼黑8549860
浏览 131回答 2
2回答

www说

您可以使用该$msg变量作为数组而不是字符串来保存特定字段的错误。<?php&nbsp; &nbsp; $msg = [];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(filter_has_var(INPUT_POST,'submit')){&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; &nbsp; &nbsp; // Check Required Fields&nbsp; &nbsp; &nbsp; &nbsp; if(!empty($email) && !empty($name) && !empty($message)){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(filter_var($email, FILTER_VALIDATE_EMAIL) === false ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg['email'] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'msg' => 'Please use a valid email'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'alert-danger'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(mail($toEmail, $subject, $body, $headers)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Email Sent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg['default'] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'msg' => 'Your email has been sent'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'alert-success'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Failed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg['default'] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'msg' => 'Your email was not sent'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'alert-danger'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Failed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$msg['default'] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'msg' => 'Please fill in all fields'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'alert-danger'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }?>现在,在 HTML 中,您可以检查特定错误或消息是否存在,并将其显示在正确的位置;顶部的默认消息和电子邮件输入下的电子邮件错误:<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <link rel="stylesheet" href="style.css">&nbsp; &nbsp; <title>Contact US</title></head><body>&nbsp; &nbsp; &nbsp;<nav class="navbar">&nbsp; &nbsp; &nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="navbar-header">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="index.php" class="navbar-brand">My Website</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp;</nav>&nbsp; &nbsp; &nbsp;<div class="container">&nbsp; &nbsp; &nbsp; &nbsp; <!-- HERE -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php if(isset($msg['default'])): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="alert <?php echo $msg['default']['class']; ?>"><?php echo $msg['default']['msg']?></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- ... -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="name">Email</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="email" class="form-control" value="<?php echo isset($_POST['email']) ? $email : ''; ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- AND HERE -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if(isset($msg['email'])): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="alert <?php echo $msg['email']['class']; ?>"><?php echo $msg['email']['msg']?></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- ... -->&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp;</div></body></html>实际上,您也可以对您想要的任何其他输入执行相同的操作。

ITMISS

您可以将验证警告存储在数组类型变量中,而不是字符串中。例如,这一行:$msg = '请使用有效的电子邮件';你可以这样做:$msg['email'] = 'Please use a valid email';另外,我认为您想验证每个必填字段是否都存在单独的if检查,而不是全部一起存在,例如:if(!empty($email) && !empty($name) && !empty($message)){您可以实施为:if (empty($email) || filter_var($email, FILTER_VALIDATE_EMAIL) === false) {&nbsp; &nbsp; $msg['email'] = 'Please use a valid email';}if (empty($name)) {&nbsp; &nbsp; $msg['name'] = 'Please enter a name';}if (empty($message)) {&nbsp; &nbsp; $msg['message'] = 'Please enter a message';}然后,在 HTML 部分的每个相关输入字段下方,您可以添加:<div class="form-group">&nbsp; &nbsp; <label for="name">Name</label>&nbsp;&nbsp; &nbsp; <input type="text" name="name" class="form-control" value="<?php echo isset($_POST['name']) ? $name : ''; ?>">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <?php if(isset($msg['name'])): ?>&nbsp; &nbsp; &nbsp; &nbsp;<p> <?php echo $msg['name']; ?>&nbsp; &nbsp; <?php endif; ?></div>然而,总的来说,我不得不说这是一种非常老式的 PHP 使用方式,您可以将逻辑代码与显示代码混合在一起。当你掌握了它的窍门后,也许你会想研究 Laravel、Symfony 甚至 CodeIgniter 是如何工作的,在那里你可以使用所谓的 MVC 将显示与逻辑分开。此外,您最终可以使用像 Blade 或类似的模板引擎来回显您的变量。
打开App,查看更多内容
随时随地看视频慕课网APP