error-handling - PHP Mailer问题。错误不会显示

标签 error-handling phpmailer

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

//侧面问题//
我遇到的问题是用户按下“发送”,而不是将表单停留在同一位置,而是页面跳回到顶部。有谁知道我如何允许它保留在表格上

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

谢谢,我期待着大家的回音:)

最佳答案

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

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

关于error-handling - PHP Mailer问题。错误不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29248518/

相关文章:

php - 如何在phplist中发送个性化邮件

email - MIXED_ES 太多 es 不是 es

node.js - Node Js - 如果连接中断,Mongoose find 不会抛出错误

python - 转换字节->字符串->返回字节,并获取原始值

android - RelativeLayout中不能存在循环依赖项吗?

phpMailer plesk centos 多IP设置

php - 为什么通过 php 发送的电子邮件中的文本被换行?

rest - Powershell正在吞噬REST错误响应

ruby-on-rails - 设计:可锁定 - last_attempt_warning 不显示

php - 如何禁用 phpmailer 消息