如何在提交联系表单后阻止PHP文件弹出?

时间:2013-08-09 22:17:44

标签: php javascript html5 web

现在,每当我点击我网站上联系表格上的提交按钮时,它就会将我带到www.mysite.com/php/function/email.php页面,而不是在后台运行email.php就像我想要的那样。

该网站应该允许您提交联系表格,然后该按钮将消失并淡入“您的消息已被发送!”,然后淡出按钮。

HTML:

<form name="contact form" id='contact_form' method="post" action="php/function/email.php">
<div class="row half">
    <div class="6u">
        <input name="name" placeholder="Name" type="text" class="text" />
    </div>
    <div class="6u">
        <input name="email" placeholder="Email" type="email" class="text" />
    </div>
</div>
<div class="row half">
    <div class="12u">
        <textarea name="message" placeholder="Message"></textarea>
    </div>
</div>
<div class="row half">
    <div class="12u">
        <input id="submit" name="submit" type="submit" value="Send Message" class="button button-icon icon icon-envelope" />
    </div>
</div>
<p id="success" style="display:none">Your message has been sent! I'll get back to you soon!</p>
        <p id="error" style="display:none">Please fill in all the fields and try again!</p>

JS:

 $(document).ready(function(){
    $('#send_message').click(function(e){

        //Stop form submission & check the validation
        e.preventDefault();

        // Variable declaration
        var name = $('#name').val();
        var email = $('#email').val();
        var subject = $('#subject').val();
        var message = $('#message').val();

       // Disable submit button just after the form processed 1st time successfully.
        $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });

        /* Post Ajax function of jQuery to get all the data from the submission of the form as soon as the form sends the values to email.php*/
        $.post("email.php", $("#contact_form").serialize(),function(result){
            //Check the result set from email.php file.
            if(result == 'sent'){
                //If the email is sent successfully, remove the submit button
                 $('#submit').remove();
                //Display the success message
                $('#success').fadeIn(500);
            }else{
                //Display the error message
                $('#error').fadeIn(500);
                // Enable the submit button again
                $('#submit').removeAttr('disabled').attr('value', 'Send The Message');
            }
        });
    }
});    
};

PHP:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Example'; 
$to = 'example@gmail.com'; 
$subject = 'Example Contact Form';

$body = "
From: $name
Email: $email
---------------
Message: \n
$message";
if ($_POST['submit']) {              
    if (mail ($to, $subject, $body, $from)) { 
    echo 'sent';
} else { 
    echo 'failed';
}
}
?>

2 个答案:

答案 0 :(得分:2)

问题是您正在#send_message上查找点击事件,但您的提交按钮的ID为submit。所以点击事件永远不会发生。

答案 1 :(得分:2)

.click()的{​​{1}}事件实际上并不是导致重定向的原因。它改为<input type="submit">的{​​{3}}。

您可以.submit() event,因此无法访问<form>

<form>

//Stop form submission & check the validation e.stopPropagation(); preventDefault()的{​​{1}}。

<form>

注意:您还应考虑将代码移至.submit()处理程序,因为大多数浏览器都支持提交$('#contact_form').submit(function (e) { e.preventDefault(); }); 的其他方式,而不是实际点击.submit()