Error handling PHP邮件问题。错误将不会显示

Error handling PHP邮件问题。错误将不会显示,error-handling,phpmailer,Error Handling,Phpmailer,我试图让我的php邮件程序正常工作,同时在有人没有填写信息时显示错误。我让它工作到一定程度,但现在错误不会出现 //附带问题 我遇到的问题是,用户按下Send键,而不是停留在表单所在的同一位置,页面跳回顶部。有人知道我怎么能让它留在表格上吗 PHP include("includes/config.php"); if(isset($_POST['submit'])) { //echo "submit was pressed"; i

我试图让我的php邮件程序正常工作,同时在有人没有填写信息时显示错误。我让它工作到一定程度,但现在错误不会出现

//附带问题 我遇到的问题是,用户按下Send键,而不是停留在表单所在的同一位置,页面跳回顶部。有人知道我怎么能让它留在表格上吗

PHP
    include("includes/config.php");

    if(isset($_POST['submit']))
    {
        //echo "submit was pressed";    
        if(strlen($_POST['firstname']) > 1)
        {
            $firstname = $_POST['firstname'];
            //echo $firstname;
        }
        else
        {
            $errors['firstname'] = "<span class=\"error\">Please enter a firstname</span>";
        }

        if(strlen($_POST['email']) > 1)
        {
            $email = $_POST['email'];
            //echo $email;

            if(filter_var($email, FILTER_VALIDATE_EMAIL))
            {
                $email = $email;
            }
            else
            {
                $errors['email'] = "<span class=\"error\">Please enter a VALID email address</span>";
            }
        }
        else
        {
            $errors['email'] = "<span class=\"error\">Please enter an email address</span>";
        }

        if(strlen($_POST['comments']) > 1)
        {
            $comments = $_POST['comments'];
            //echo $comments;
        }
        else
        {
            $errors['comments'] = "<span class=\"error\">Please enter a comment</span>";
        }

        if($errors < 1)
        {
            include("includes/class.phpmailer.php");
            $mail = new PHPMailer;

            $to = "erin@gmail.com";
            $from = $email;
            $from_name = $firstname;
            $subject = "You've got mail from " . $firstname;
            $body = "Their comments <br/><br/>" . $comments; 

            $mail->From = $from;
            $mail->FromName = $from_name;
            $mail->addAddress($to, 'Erin');
            $mail->addReplyTo($from, $from_name);
            $mail->isHTML(true);  

            $mail->Subject = $subject;
            $mail->Body    = $body;

            if(!$mail->send()) {
                echo 'Message could not be sent.';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            } else {
                //echo 'Message has been sent';
                $success = "<p class=\"success\">Your message has been sent successfully.</p>";

                unset($firstname);
                unset($email);
                unset($comments);
            }
        }
    }

?>

谢谢,我期待听到大家的声音:)

您的页面将产生很多错误,因为加载时您引用了未定义的变量-例如,
$errors['firstname']
在首次加载页面时未定义,但您仍要求它显示。这些错误应该正常显示-如果不是,您可能已经关闭了错误报告,这在开发时不是一个好主意,因此您应该检查web服务器日志以查看任何其他错误


它“跳回顶部”,因为它正在重新加载页面。如果您通过ajax请求提交表单,则不会发生这种情况。

谢谢,但请您详细介绍一下。我理解你的意思,但我不知道在php.ini文件中需要在哪里实现:查看
display\u errors
error\u reporting
<div id="form-div">
    <?= $success; ?>

    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">

      <p class="name">
      <?= $errors['firstname']; ?><br>
        <input name="firstname" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" value="<?= $firstname; ?>" />
      </p>

      <p class="email">
      <?= $errors['email']; ?><br>
        <input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" value="<?= $email; ?>"/>
      </p>

      <p class="text">
      <?= $errors['comments']; ?><br>
        <textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"><?= $comments; ?></textarea>
      </p>


      <div class="submit">
        <input type="submit" value="SEND" id="button-blue"/>
        <div class="ease"></div>
      </div>
    </form>
  </div>
/* ============================================================
  CONTACT FORM
============================================================ */
@import url(http://fonts.googleapis.com/css?family=Montserrat:400,700);
#form-div {
    background-color:rgba(72,72,72,0.4);
    padding-left: 25px;
    padding-right: 25px;
    padding-top: 25px;
    padding-bottom: 25px;
    width: 450px;
    float: left;
    left: 50%;
    position: absolute;
    margin-top: 15px;
    margin-bottom: 10px;
    margin-left: -260px;
    -moz-border-radius: 7px;
    -webkit-border-radius: 7px;
}

.feedback-input {
    color:#3c3c3c;
    font-family: 'Open Sans', sans-serif;
    font-weight:500;
    font-size: 18px;
    border-radius: 0;
    line-height: 22px;
    background-color: #fbfbfb;
    padding: 13px 13px 13px 54px;
    width:100%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
    border: 3px solid rgba(0,0,0,0);
}

.feedback-input:focus{
    background: #fff;
    box-shadow: 0;
    border: 3px solid #3498db;
    color: #3498db;
    outline: none;
  padding: 13px 13px 13px 54px;
}

.focused{
    color:#30aed6;
    border:#30aed6 solid 3px;
}

/* ICONS */
#name{
    background-image: url(http://rexkirby.com/kirbyandson/images/name.svg);
    background-size: 30px 30px;
    background-position: 11px 8px;
    background-repeat: no-repeat;
}

#name:focus{
    background-image: url(http://rexkirby.com/kirbyandson/images/name.svg);
    background-size: 30px 30px;
    background-position: 8px 5px;
  background-position: 11px 8px;
    background-repeat: no-repeat;
}

#email{
    background-image: url(http://rexkirby.com/kirbyandson/images/email.svg);
    background-size: 30px 30px;
    background-position: 11px 8px;
    background-repeat: no-repeat;
}

#email:focus{
    background-image: url(http://rexkirby.com/kirbyandson/images/email.svg);
    background-size: 30px 30px;
  background-position: 11px 8px;
    background-repeat: no-repeat;
}

#comment{
    background-image: url(http://rexkirby.com/kirbyandson/images/comment.svg);
    background-size: 30px 30px;
    background-position: 11px 8px;
    background-repeat: no-repeat;
}

textarea {
    width: 100%;
    height: 150px;
    line-height: 150%;
    resize:vertical;
}

input:hover, textarea:hover,
input:focus, textarea:focus {
    background-color:white;
}

#button-blue{
    font-family: 'Montserrat', Arial, Helvetica, sans-serif;
    float:left;
    width: 100%;
    border: #fbfbfb solid 4px;
    cursor:pointer;
    background-color: #3498db;
    color:white;
    font-size:24px;
    padding-top:22px;
    padding-bottom:22px;
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    transition: all 0.3s;
  margin-top:-4px;
  font-weight:700;
}

#button-blue:hover{
    background-color: rgba(0,0,0,0);
    color: #0493bd;
}

.submit:hover {
    color: #3498db;
}

.ease {
    width: 0px;
    height: 74px;
    background-color: #fbfbfb;
    -webkit-transition: .3s ease;
    -moz-transition: .3s ease;
    -o-transition: .3s ease;
    -ms-transition: .3s ease;
    transition: .3s ease;
}

.submit:hover .ease{
  width:100%;
  background-color:white;
}

@media only screen and (max-width: 580px) {
    #form-div{
        left: 3%;
        margin-right: 3%;
        width: 88%;
        margin-left: 0;
        padding-left: 3%;
        padding-right: 3%;
    }
}