0

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. The function sends the data from the inputs to a API.php file, which calls for a sending function in the BusinessLogic.php file. Once the form is submitted - a callback function should be activated and it should animate a confirmation div.

The HTML of the inputs:

<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:

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.

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.

Why is it happening and how do I fix it? Thanks!

6
  • Could you provide the html code of the form? Commented Jan 20, 2014 at 20:18
  • Your php isn't returning JSON, resulting in a parseerror being thrown. Use $.get instead, or modify your php to have it return json. Commented Jan 20, 2014 at 20:18
  • @vooD I updated the question with the HTML. Commented Jan 20, 2014 at 20:33
  • @KevinB Tried to return JSON and to use $.get, but it still didn't activate the callback. Commented Jan 20, 2014 at 20:35
  • 1
    @vooD Problem solved, I had to use $.post instead of $.getJSON. Thanks for your input, I appreciate it! Commented Jan 20, 2014 at 20:45

1 Answer 1

2

A different approach could be using $.post instead of $.getJSON (everything else will remain the same). It will make the desired ajax call. Parameters defined will be in $_POST array ($_REQUEST is fine)

Sign up to request clarification or add additional context in comments.

2 Comments

It worked, thanks! Interestingly enough, this exact code works for me in my other forms, I just reused it and made some proper adjustments, but it didn't work this time for some reason. Again, thanks!
Cross-domain issues perhaps?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.