0

I cant understand why the following is not working, I am trying to call a php script to send an email, but i dont think my ajax call is calling the php script at all, I just get an error returned.

My ajax call:

        var name_field = $('#cf_name').val();
        $.ajax({
            type: 'post',
            url: 'contact.php',
            data: {'name_field' : name_field },
            dataType: 'json',
            success: function(data) {
                if(data.status == 'success')
                    alert("Thank you for subscribing!");
                else if(data.status == 'error')
                    alert("Error on query!");
            },
            error: function(err) {
                alert(err.responseText);
            }
        });

My php script:

$field_name =  isset($_POST['name_field']);
$field_email = 'name@email.com';
$field_phone = '12345';
$field_message = 'Hello World!';

$mail_to = 'emailaddress@email.com';
$subject = 'Message from a website visitor '.$field_name;

$body_message = 'This message has been sent via website:'."\n\n";
$body_message .= 'From: '.$field_name."\n\n";
$body_message .= 'E-mail: '.$field_email."\n\n";
$body_message .= 'Phone Number: '.$field_phone."\n\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { 
    $response_array['status'] = 'success';  
}
else { 
    $response_array['status'] = 'error';
}
header('Content-type: application/json');
echo json_encode($response_array);

If i navigate to the url directly the php works as intended, but i can not get the ajax post to call it.

display-name-is-missing
  • 4,424
  • 5
  • 28
  • 41
LolBB
  • 55
  • 1
  • 9
  • What happens when you try the ajax? Do you get to the ajax line at all? Do you get to the success function? Do you get to the error function? – Damien Black Feb 10 '14 at 23:26
  • Use the devtools to see the status of the ajax request in the network tab (firebug/fox/chrome) and see if helps or paste the result in your question, also try to use an absolute URL, if you are getting 404 your url isn't right. – Allende Feb 10 '14 at 23:27
  • I definitely hit the error response from the ajax call. Although the err.responseText is empty. – LolBB Feb 10 '14 at 23:29
  • The devtools network tab - the response is (cancelled) ?? – LolBB Feb 10 '14 at 23:34
  • But, it's your ajax call actually reaching your php script ? If something it's wrong in the server side (php) you will see a 500 status code in the response of your request in the network tab on the devtool, if you see a 404 you're not even reaching your script, if you get a 200 response then you're half-way. – Allende Feb 10 '14 at 23:35
  • See this: http://stackoverflow.com/questions/10990607/jquery-ajax-post-canceled i'm not a js expert but I agree with your problem being the sync execution of your ajax call, since it take a while to send an email, maybe isn't completed in time for your js – Allende Feb 10 '14 at 23:37
  • Thanks! Adding 'async: false' to my ajax call seems to have sorted it. I'm very thankful its working, but does anyone know why? – LolBB Feb 10 '14 at 23:43
  • Not able to explain (as told you before not a js expert) but it's related toyour js keeps its execution no matter if your server side code is or isn't completed. Posting my commment as answer :) in case you want it to accept it or wait for a full explanation from someone else – Allende Feb 10 '14 at 23:47

3 Answers3

1

$field_name is not an actual string, it's a boolean since you used $field_name=isset($_POST[name_field']). Remove the isset().

Joe Thomas
  • 5,807
  • 6
  • 25
  • 36
0

You should check url: contact.php as you did not specify the full URL, so your post is going to a relative path. The root path will be same current script path.

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Minhaz
  • 446
  • 5
  • 7
0

See this: jQuery Ajax post cancelled i'm not a js expert but I agree with your problem being the async execution of your ajax call, since it takes a while to send an email, maybe isn't completed in time for your js

See this nice explanation about the "async" propertie in the ajax call:

What does "async: false" do in jQuery.ajax()?

And see this question/answer:

How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

Community
  • 1
  • 1
Allende
  • 1,480
  • 2
  • 22
  • 39