表单提交无效

时间:2015-07-25 16:53:22

标签: php jquery html forms email

我创建了一个简单的联系表单,其中包含姓名,号码,电子邮件和消息字段 为了验证,我使用了一个名为 jQuery Validation Plugin 的jQuery插件。
对于实际发送邮件,我使用了一个名为 PHPMailer 的插件。
我好像写的所有代码都没有错误,但是我没有收到邮件(我试图从 localhost 发送它,但我确保一切都能正常工作。使用 XAMPP 的)。

现在,这是代码:
HTML:

<form id="mailSender" method="post" action="index.php" novalidate>
        <div id="nameDiv">
        <input type="text" id="name" name="name" placeholder="Your Name" onblur="name_redder('black')" onfocus="name_redder('#F8CB18')" onkeyup="name_redder('#F8CB18')" />
        </div>

        <div id="numDiv">
        <input type="text" id="number" name="number" placeholder="Your Mobile Number(Optional)" onblur="num_redder('black')" onfocus="num_redder('#F8CB18')" onkeyup="num_redder('#F8CB18')" />
        </div>

        <div id="mailDiv">
        <input type="email" id="email" name="email" placeholder="Your Email" onblur="mail_redder('black')" onfocus="mail_redder('#F8CB18')" onkeyup="mail_redder('#F8CB18')" />
        </div>

        <div id="msgDiv">
        <textarea id="message" name="message" placeholder="Your Message" class="mCustomScrollbar" data-mcs-theme="minimal-dark" data-mcs-axis="y" onblur="message_redder('black')" onfocus="message_redder('#F8CB18')" onkeyup="message_redder('#F8CB18')"></textarea>
        </div>
        <br>
        <input type="submit" id="contact_submit" name="contact_submit" value="Send Message">
</form>

jQuery验证插件代码:

<script>
$("#mailSender").validate({
    rules: {
        name: {
            required: true,
            minlength: 2,
            lettersonly: true
        },
        number: {
            required: false,
            minlength: 7,
            maxlength: 9,
            numbersonly: true
        },
        email: {
            required: true,
            email: true
        },
        message: {
            required: true,
            minlength: 15,
            msgonly: true
        }
    },
    messages: {
        name: {
            required: "This field is required.",
            minlength: "Please enter a valid name.",
        },
        email: {
            required: "This field is required.",
            email: "Please enter a valid email address."
        },
        message: {
            required: "This field is required.",
            minlength: "Please enter at least 10 characters."
        }
    }
});
$.validator.addMethod("lettersonly", function(value, element) {
    return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only, please.");
$.validator.addMethod("numbersonly", function(value, element) {
    return this.optional(element) || /^[0-9]+$/i.test(value);
}, "Numbers only, please.");
$.validator.addMethod("msgonly", function(value, element) {
    return this.optional(element) || /^[a-z\-.,()!?'"\s^0-9]+$/i.test(value);
}, "Letters, numbers or punctuation only, please.");
</script>

PHP:

function has_header_injection($str) {
            return preg_match("/[\r\n]/", $str);
        }

        if (isset($_POST['contact_submit'])) {
            $name = trim($_POST['name']);
            $numb = trim($_POST['number']);
            $email = trim($_POST['email']);
            $msg = $_POST['message'];

            if(has_header_injection($name || $email || $numb || $msg)) {
                die();
            }

            $mail = new PHPMailer;

            //$mail->SMTPDebug = 3;                               // Enable verbose debug output

            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'myEmail@gmail.com';                 // SMTP username
            $mail->Password = 'theOriginalFileOnMyPCContainsMyPasswordHere';                           // SMTP password
            $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to

            $mail->From = 'from@example.com';
            $mail->FromName = 'Mailer';
            $mail->addAddress('dasabrix@gmail.com', 'Joe User');     // Add a recipient
            $mail->addAddress('ellen@example.com');               // Name is optional
            $mail->addReplyTo('info@example.com', 'Information');
            $mail->addCC('cc@example.com');
            $mail->addBCC('bcc@example.com');

            $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
            $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
            $mail->isHTML(true);                                  // Set email format to HTML

            $mail->Subject = 'Here is the subject';
            $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            if(!$mail->send()) {
                echo 'Message could not be sent.';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            } else {
                echo 'Message has been sent';
            }
}

正如我之前所说,它不起作用。我尝试将header("Location:index.php");放在else内,但它也没有带我到index.php。所以我认为这可能是我提交表单的方式的错误。
任何人都可以提供帮助吗?

更新:我忘了说是的,我已经加入 PHPMailer 插件:require '../js/phpmailer/PHPMailerAutoload.php';。它包含在header.php之后,作为contact.php中的第一件事之一。

我还将网站上传到网上并从那里尝试过。仍然无效。

我还包括class.phpmailer.php。这也不起作用。

0 个答案:

没有答案