淡出电子邮件表格

时间:2013-12-28 09:41:42

标签: javascript php jquery email twitter-bootstrap

我试图在提交时淡出一个自举联系表单。

我正在使用我发现的代码(我已经稍微修改了以满足我的需要),而我的实现也遇到了问题。我很新,我似乎已经陷入困境。

这是JS:

$('contactUs').on('submit', function mailMe(form) {
    form.preventDefault(); //Prevents default submit
    var form = $(this); 
    var post_url = form.attr('action'); 
    var post_data = form.serialize(); //Serialized the form data for process.php
    $('#loader', form).html('<img src="http://domain.com/test/images/loading.gif" /> Please Wait...');
    $.ajax({
        type: 'POST',
        url: 'http://domain.com/test/process.php', // Your form script
        data: post_data,
        success: function(msg) {
            $(form).fadeOut(500, function(){
                form.html(msg).fadeIn();
            });
        }
    });
});

这是表格:

<form name="contactUs" onSubmit="return mailMe(this.form)" >
    <div class="inputWrap">
        <div class="fname">
            <input class="myInput miLeft" type="text" placeholder="Name">
        </div>
        <div class="femail">
            <input class="myInput miRight" type="text" placeholder="Email">
        </div>
    </div>
    <div class="taWrap">
            <textarea class="myTa" type="text" placeholder="Message"></textarea>
    </div>
    <button class="btns btn-3 btn-3g btnsx">Send</button>
</form>

这是process.php:

<?php

/* Configuration */
$subject = 'New Customer Email'; // Set email subject line here
$mailto  = 'myemail@me.com'; // Email address to send form submission to
/* END Configuration */

$name           = $_POST['name'];
$email          = $_POST['email'];
$messageContent      = $_POST['messageContent'];
$timestamp = date("F jS Y, h:iA.", time());

// HTML for email to send submission details
$body = "
<br>
<p>The following information was submitted through the contact form on your website:</p>
<p><b>Name</b>: $name<br>
<b>Email</b>: $email<br>
<b>Message</b>: $messageContent<br>
<p>This form was submitted on <b>$timestamp</b></p>
";

// Success Message
$success = "
<div class=\"row-fluid\">
    <div class=\"span12\">
        <h3>Submission successful</h3>
        <p>Thank you for taking the time to contact Shaz Construction & Design. A representative will be in contact with you shortly. If you need immediate assistance or would like to speak to someone now, please feel free to contact us directly at <strong>(415) 382-8442</strong>.</p>
    </div>
</div>
";

$headers = "From: $name <$email> \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>$body</body></html>";

if (mail($mailto, $subject, $message, $headers)) {
    echo "$success"; // success
} else {
    echo 'Form submission failed. Please try again...'; // failure
}

?>

1 个答案:

答案 0 :(得分:2)

您遗失了一些小事:

  • 表单的jQuery选择器不正确 - 为表单提供contactUs的ID属性,然后使用选择器$('form#contactUs')。摆脱表单上的name属性。
  • 您的按钮元素必须是提交类型 - 您的按钮当前不执行任何操作。
  • 您不需要onSubmit属性,您已经将表单绑定到JS中的事件。
  • 您的输入标记目前没有任何名称元素 - 它们是必需的 - 请参阅http://api.jquery.com/serialize/
  • 您尝试访问不存在的表单上的属性(操作),但您不使用它,所以只需删除该行。
  • 在事件处理程序中使用return false而不是preventDefault(我无法使preventDefault工作。但这可能只是我!)
  • 由于您的代码的上下文,我无法告诉您,但请确保您的JS位于$('document').ready(function() { ... }块内。

我认为你的JS和HTML应该是:

<强> JS

$('form#contactUs').on('submit', function() {
    var form = $(this); 
    var post_data = form.serialize(); //Serialized the form data for process.php
    $('#loader').html('<img src="http://yasharsahaleh.com/test/images/loading.gif" /> Please Wait...');
    $.ajax({
        type: 'POST',
        url: 'http://yasharsahaleh.com/test/process.php', // Your form script
        data: post_data,
        success: function(msg) {
            $('#loader').html('');

            // We know this is the form that needs fading in/out
            $('form#contactUs').fadeOut(500, function(){
                $('form#contactUs').html(msg).fadeIn();
            });
        }
    });

    return false;
});

<强> HTML

<form id="contactUs">
    <div class="inputWrap">
        <div class="fname">
            <input name="name" class="myInput miLeft" type="text" placeholder="Name">
        </div>
        <div class="femail">
            <input name="email" class="myInput miRight" type="text" placeholder="Email">
        </div>
    </div>
    <div class="taWrap">
            <textarea name="messageContent" class="myTa" type="text" placeholder="Message"></textarea>
    </div>
    <button type="submit" class="btns btn-3 btn-3g btnsx">Send</button>
</form>

我做了一个小的JSFiddle来说明大部分内容(取出AJAX部分):http://jsfiddle.net/dualspiral/2rXas/1/

PHP需要稍微改变,你实际上并没有打印出变量内容。实际上会分配身体变量:

$body = "
    <br>
    <p>The following information was submitted through the contact form on your website:</p>
    <p><b>Name</b>: " . $name . "<br>
    <b>Email</b>: " . $email . "<br>
    <b>Message</b>: " . $messageContent . "<br>
    <p>This form was submitted on <b>" . $timestamp . "</b></p>
";

,最后一行应为:

$headers = "From: " . $name . " <" . $email . "> \r\n";
$headers .= "Reply-To: " . $email . " \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>" . $body . "</body></html>";

if (mail($mailto, $subject, $message, $headers)) {
    echo $success; // success
} else {
    echo 'Form submission failed. Please try again...'; // failure
}

?>