【问题标题】:Fadeout Email Form淡出电子邮件表单
【发布时间】:2014-01-15 18:37:04
【问题描述】:

我正在尝试让引导联系表单在提交时淡出。

我正在使用我找到的代码(我已经稍微修改以适应我的需要),但我在执行它时遇到了麻烦。我还很新,我似乎陷入了困境。

这里是 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
}

?>

【问题讨论】:

  • 你遇到了什么错误?
  • 似乎什么都没有发生,这让我意识到我在这里遗漏了一些重要的东西。页面只是重新加载。表格是here

标签: javascript php jquery email twitter-bootstrap


【解决方案1】:

您缺少一些小东西:

  • 您的表单的 jQuery 选择器不正确 - 给您的表单一个 ID 属性 contactUs,然后使用选择器 $('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 变量实际上应该被赋值:

$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
}

?>

【讨论】:

  • 非常感谢!我似乎已经全部完成了,但是我的表单似乎在每次提交时都失败了(无论数据是否有效)。我有form setup here
  • 不客气。至于您的表单,您的邮件功能返回 false。我不太了解它,但我怀疑您的主机可能限制使用该功能,或者它配置错误。除了指出php.net/manual/en/function.mail.phpthis SO question 之外,我真的无能为力。祝你好运!
猜你喜欢
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 2012-07-02
  • 2011-10-07
  • 1970-01-01
相关资源
最近更新 更多