现在,每当我单击网站上联系表单上的“提交”按钮时,它就会带我到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';
}
}
?>

最佳答案

.click()<input type="submit">事件实际上并不是导致重定向的原因。相反,它在.submit()处成为<form> event

您可以e.stopPropagation()之一,这样它就不会到达<form>

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


preventDefault()<form>中的.submit()

$('#contact_form').submit(function (e) {
    e.preventDefault();
});


注意:您还应该考虑将代码移到.submit()处理程序中,因为大多数浏览器支持其他方式来提交<form>,而不是实际单击<input type="submit">

10-08 08:12
查看更多