【问题标题】:Ajax not submitting contact formAjax 未提交联系表单
【发布时间】:2013-06-28 22:09:32
【问题描述】:

我以前从未创建过联系表单,并且由于某种原因无法提交此表单。它验证得很好,但是当我单击提交时,它什么也不做。没有错误。没有什么。我已经为此工作了 12 多个小时,而在我的一生中,我无法弄清楚出了什么问题。这是包含 AJAX 代码的 jquery 代码。

$(document).ready(function () {
    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "ajaxSubmit.php",
        ajaxSubmitMessage: "Thank you, We will contact you soon !",
        inlineValidation: false,
        success: function () {
            callSuccessFunction()
        },
        failure: function () {}
    })
});

这是html页面上表单的代码

<form class="form" id="form1" method="post" action="ajaxSubmit.php">   
    <input class="validate[required,custom[onlyLetter],length[0,100]] text-input" type="text" name="name" id="name"  placeholder="How may I address you?"><label for='name '>name</label><br />
    <input class="validate[required,custom[email]] text-input" type="text" name="email" id="email" placeholder="I promise, I hate spam as much as you do."><label for='email '>email</label><br />
    <input class="validate[required,length[0,100]] text-input" type="text" name="budget" id="budget" placeholder="$USD amount, please."><label for='budget '>budget</label><br />
    <textarea name="message" class="validate[required,length[6,300]] text-input" id="comment" placeholder="What's on your mind?"></textarea><br />
    <p class="submit"><input type="submit" value="Send"></p>
</form>

这是完整的“ajaxSubmit.php 代码

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$budget = $_POST['budget'];
$body = $_POST['text'];
$receiver = "inquiry@seednouveau.com";
if (!empty($name) & !empty($email) && !empty($body)) {
    $body = "Name:{$name}\n\nBudget :{$budget}\n\nComments:{$body}";
    $send = mail($email, 'Contact Form Submission', $body, "From: {$email}");
    if ($send) {
        echo 'true';
    }

}

?>

我把头发拉出来是因为我真的希望这种特殊的形式起作用,而且我非常接近。我喜欢它在同一页面上验证的方式,而不是在用户尝试提交错误填写的表单之后。我真的不希望我的用户输入整封电子邮件然后因为他们没有输入内容或输入错误而丢失。

【问题讨论】:

  • 感谢您的编辑 Shinosha。不幸的是,表格仍然无法正常工作。任何想法为什么?

标签: ajax jquery form-submit contact-form jquery-validation-engine


【解决方案1】:

在进一步研究之后,还有另一个错误肯定会给您带来麻烦。您的表单包含名为

的字段
name, email, budget, message

很遗憾,您的 PHP 页面正在寻找名为

的字段
name, email, budget, text

您的 PHP 脚本将无法找到名为“text”的字段,因此您的表单似乎正在提交,但您的 PHP 脚本没有返回让库思考所需的“true”它完全完成了。如果您解决了消息/文本命名问题,您应该更接近工作往返。

【讨论】:

    【解决方案2】:

    假设你使用的是2.6.1版本的jquery-validation-engine,没有“onlyLetter”,需要改成“onlyLetterSp”。

    <input class="validate[required,custom[onlyLetter],length[0,100]] text-input" type="text" name="name" id="name"  placeholder="How may I address you?"><label for='name '>name</label><br />
    

    到

    <input class="validate[required,custom[onlyLetterSp],length[0,100]] text-input" type="text" name="name" id="name"  placeholder="How may I address you?"><label for='name '>name</label><br />
    

    【讨论】:

    • 嘿@SheldonGriffin。谢谢回复。这是我没有包含在顶部的脚本。我认为它使用的是旧版本的 jquery。 1.3,对吧?
    • 再次感谢@SheldonGriffin!我们还发现需要将“callSuccessFunction”替换为 REAL 调用函数。我们使用这个在成功提交后将浏览器重定向到感谢页面。成功:函数(){window.location.href ='seednouveau.com/ThankYou.html'; }, failure: function () {} 将整行一起删除还显示了原始代码中最初打算使用的正确 AJAX 消息发送警报。再次感谢您!
    • 对不起。我是 StackOverflow 的新手。还不确定如何正确格式化这些消息!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多