为什么我的提交表单将我带到我的php页面?

时间:2013-03-02 02:38:07

标签: php javascript jquery ajax forms

(我没有意识到我之前已经发过的帖子可以编辑并重新打开。对这个过程感到困惑......)

当我点击发送表单时,电子邮件发送正确,但后来我被发送到我的php页面显示数字0.我不明白为什么会发生这种情况。这是(大部分)完整代码:

<form id="contactForm" method="post" action="js/formCheck.php">
    <div class="formCol">
        <label>Email:</label>
        <input type="email" id="formEmail" name="email" autocomplete="off" placeholder="Your email address..." required/>
    </div>
    <div class="formCol midCol">
        <label id="floatMsg">Message (350 chars):</label>
        <textarea id="formMessage" name="message" rows="5" cols="40" maxlength="350" placeholder="What have you got to say?" required></textarea>

    </div>
    <div class="formCol lastCol">
        <input type="submit" class="submit-btn" name="submit" value="Send" />
    </div>
</form>

...脚本

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!--[if lt IE 9]><script src="js/modernizr.custom.js"></script><![endif]-->
<script src="js/jquery.flexslider-min.js"></script>
<script src="js/functions.js"></script>
<script>
    $(document).ready(function() {
        $('#form').ajaxForm(function() {
            alert("Thank you for your comment! I will receive it shortly and get back to you as soon as possible.");
            document.getElementById("form").reset();
        });

    });
</script>

发送电子邮件(PHP)

if (isset($_POST['submit'])) {
    $email = htmlspecialchars($_POST['email']);
    $comment = htmlspecialchars($_POST['message']);



    /* Sending Email */
    $from_add = "Visitor";
    $to_add = "user@email.com";
    $subject = "The_Message_Subject";
    $message = "
        Message Information
        Email = $email
        Message = $comment";

    $headers = "From: $from_add \r\n";
    $headers .= "Reply-To: $from_add \r\n";
    $headers .= "Return-Path: $from_add \r\n";
    $headers .= "X-Mailer: PHP \r\n";

    mail($to_add, $subject, $message, $headers);
}

1 个答案:

答案 0 :(得分:4)

在表单重置后添加return false。另外,表单ID为contactForm,而不仅仅是form

<script>
    $(document).ready(function() {
        $('#contactForm').ajaxForm(function() {
            alert("Thank you for your comment! I will receive it shortly and get back to you as soon as possible.");
            document.getElementById("form").reset();
            return false;
        });
    });
</script>