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

时间:2015-03-25 06:02:48

标签: error-handling phpmailer

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

// Side Issue // 我有问题,用户按下发送,而不是停留在表单所在的相同位置,页面跳回到顶部。有谁知道我怎么能让它留在表格

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);
            }
        }
    }

?>

HTML表单

<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>

CSS(如果有帮助)

/* ============================================================
  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%;
    }
}

谢谢,我期待收到你们所有人的回复:)

1 个答案:

答案 0 :(得分:0)

您的页面会产生很多错误,因为当它加载时您引用了未定义的变量 - 例如,{I}首次加载页面时未定义$errors['firstname']但是无论如何,你要它显示它。这些错误应该正常显示 - 如果它们不是您可能已关闭错误报告,这在开发过程中是个好主意,因此您应该检查您的Web服务器日志以查看任何其他错误。< / p>

它跳回到顶部&#34;因为它正在重新加载页面。如果您通过ajax请求提交表单,则不会发生。