简体   繁体   English

jQuery回调函数未执行

[英]jQuery callback function not executed

I have a contact form with a few input fields and a div that acts as a button - when I click on it, it calls for a function that validates the form and if everything is OK it submits it. 我有一个带有几个输入字段的联系表单和一个充当按钮的div - 当我点击它时,它会调用一个验证表单的函数,如果一切正常则提交它。 The function sends the data from the inputs to a API.php file, which calls for a sending function in the BusinessLogic.php file. 该函数将数据从输入发送到API.php文件,该文件调用BusinessLogic.php文件中的发送函数。 Once the form is submitted - a callback function should be activated and it should animate a confirmation div. 提交表单后 - 应激活一个回调函数,它应该为确认div设置动画。

The HTML of the inputs: 输入的HTML:

<div id="contact_inputs_block">
    <div class="div_contact_form_input_box div_input_name">
        <input type="text" name="Name" placeholder="* Name" id="name">
    </div>
    <div class="div_contact_form_input_box div_input_phone">
        <input type="tel" name="Phone" placeholder="* Phone" id="phone">
    </div>
    <div class="div_contact_form_input_box div_input_email">
        <input type="email" name="Email" placeholder="* Email" id="email">
    </div>
</div>
<div class="form_confirmation_wraper">
    <div id="div_form_confirmation">
        Message sent.
    </div>
    <div class="div_send_form">

    </div>
</div>

Here's my function: 这是我的功能:

function submit_form(language)
{   

var email_str = $("#email").val();
var errors = new Array();

$("#contact_inputs_block>div:not(:last-child)").css({'border-color' : '#a5957e', 'background-color' : '#f8f8f8'});


if ($("#name").val()=="")
{
    errors.push(".div_input_name");
}
if(!validate_email(email_str))
{
    errors.push(".div_input_email");
}
if ($("#phone").val()=="")
{
    errors.push(".div_input_phone");
}


if (errors.length == 0)
{


    $.getJSON("inc/API.php",
    {
        command : "send_contact_form",
        name : $("#name").val(),
        email : email_str,
        phone : $("#phone").val(),
        message : $("#message").val().replace(/\n/g, '<br>'),
        form_language: language
    } ,function(){
        alert("sent");
        $("#div_form_confirmation").animate({opacity:1}, 1000, function(){
            setTimeout($("#div_form_confirmation").animate({opacity:0}, 3000, function(){location.reload()}),6000);
        }); // end callback function
    }); // end getJSON
} // end if
else
{
    for (var i = 0; i <= errors.length; i++)
    {
        $(errors[i]).css({'border-color' : '#E60005', 'background-color' : '#ffc4c9'});
    }
}
}

this is the function in API.php: 这是API.php中的函数:

include_once 'BusinessLogic.php';
session_start();

$command = $_REQUEST["command"];

switch($command)
{
    case "send_contact_form" :
        send_contact_form($_REQUEST["name"], $_REQUEST["email"], 
            $_REQUEST["phone"], $_REQUEST["message"], $_REQUEST["form_language"]);
        break;
}

and the BusinessLogic.php actually sends the mail. 并且BusinessLogic.php实际上发送邮件。

The mail is being sent, everything is OK there. 邮件正在发送,一切都很好。 The problem is the callback of submit_form() function is never fired and I don't see the confirmation that the mail was sent. 问题是从不触发submit_form()函数的回调,我没有看到邮件发送的确认。

Why is it happening and how do I fix it? 为什么会这样,我该如何解决? Thanks! 谢谢!

A different approach could be using $.post instead of $.getJSON (everything else will remain the same). 另一种方法可能是使用$ .post而不是$ .getJSON(其他一切都将保持不变)。 It will make the desired ajax call. 它将进行所需的ajax调用。 Parameters defined will be in $_POST array ($_REQUEST is fine) 定义的参数将在$ _POST数组中($ _REQUEST很好)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM