为什么此查询表单不显示错误消息?

时间:2016-08-27 13:26:24

标签: php html forms material-design

我在使用Material Design Lite Library创建的页面中有一个查询表单,该表单位于页面的最后一部分。 HTML表单代码如下:

<div class="mdl-cell mdl-cell--6-col">
     <h1 class = "enquiry--header"> Enquiry </h1>
     <form method="post" action = "index.php">
     <div class = "mdl-textfield mdl-js-textfield">
       <input type = "text" id="name" name = "name" class = "mdl-textfield__input name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
        <label class="mdl-textfield__label" for="name">Name</label>
    </div>
    <?php echo "<p class = 'message'>$errName</p>";?>
    <div class = "mdl-textfield mdl-js-textfield">
        <input type = "text" id="email" name = "email" class = "mdl-textfield__input email" value="<?php echo htmlspecialchars($_POST['email']); ?>">
        <label class="mdl-textfield__label" name = "email" for="email">Email</label>
    </div>
    <?php echo "<p class = 'message'>$errEmail</p>";?>
    <div class = "mdl-textfield mdl-js-textfield">
        <textarea type = "text" id="enquiry" name = "enquiry" class = "mdl-textfield__input enquiry"  value="<?php echo htmlspecialchars($_POST['enquiry']); ?>" name = "enquiry"></textarea>
        <label class="mdl-textfield__label" for="email">Your Enquiry</label>
    </div>
    <?php echo "<p class = 'message'>$errMessage</p>";?>
    </form>
    <div id = "submit" name = "submit" type = "submit" class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored mdl-color--grey-900  mdl-js-ripple-effect">
      Send
    </div>
    <?php echo $result;?>
</div>

PHP代码:

$name = null;
$email = null;
$message = null;
$from = null;
$to = null;
$subject = null;
$errName = null;
$errEmail = null;
$errMessage = null;
$errResult = null;
    if (isset($_POST["submit"])) {
       $name = $_POST['name'];
       $email = $_POST['email'];
       $message = $_POST['message'];
       $from = 'Contact-Us'; 
       $to = 'xyz@gmail.com'; 
       $subject = 'Enquiry';

       $body = "From: $name\n E-Mail: $email\n Message:\n $message";

       // Check if name has been entered
        if (!$_POST['name']) {
           $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'],   FILTER_VALIDATE_EMAIL)) {
           $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
           $errMessage = 'Please enter your message';
        }


    if (!$errName && !$errEmail && !$errMessage) {
        if (mail ($to, $subject, $body, $from)) {
           $result='<div class="success">Thank You! We will reply shortly</div>';
           $_POST = array();
        } else {
           $result='<div class="error">Your message could not be sent.</div>';
        }
    }
       }
   ?>

结果div不会更新,也不会在提交表单时显示错误消息,因为字段留空。我正在使用MAMP在本地进行测试。

1 个答案:

答案 0 :(得分:-2)

尝试直接在error_reporting(E_ALL)开始标记下添加<?php,以确保您的错误报告全部显示。