0

I have a few short questions. I have a form that I am sending via jQuery's Ajax, and it works how I want it to. If I disable JavaScript, the form also sends via server-side, and it works too. When I send the form via Ajax, the form sends and I have a little spinner (add a class with beforeSend), and on success I display a thank you message. Question 1 is, (this may be silly), how does Ajax determine the mail was sent successfully? (I am currently using PHPMailer to send mail from my localhost).

Or rather, is 'success' just based on being able to send the form data to the processing file? My success message is "thank you, your message has been sent", and I don't want to display this if the message was not actually sent, but Ajax displays it anyway because it was able to interact with the mail processing file. I am sending via the POST method of course, and my success function doesn't take a parameter (I am not expecting data back). My data is just form data using serialize() to package it all together.

My second question is, when I disable JS and the form sends, it uses header() to determine where to go after the mail is sent, and the header() code is in the same mail processing file that .ajax() uses in its URL. Why is it that when .ajax() sends the file it doesn't use header to go to the "thank you" page, but when JS is disabled it uses header() and goes to the thank you page? All the code is in the same file, so does it skip the header() part? Is it because the page simply doesn't reload? This may be silly but I'd just like to know what's going on here. Thanks so much.

EDIT: I should have added the code, my apologies. Here is the ajax call I am making:

$.ajax(' url', {
            type: "POST",
            data: form.serialize(),
            success: function() {
                var formWrapper  = $('.form-wrapper');
                var confirmMessageSection = $("<section class=\"confirm-message\"></section>");
                formWrapper.empty().append(confirmMessageSection);
                confirmMessageSection.html("<p>Thank you! Your message has been sent.</p>").hide().fadeIn(1500);            
            },
            error: function(request, errorType, errorMessage) {
                var formWrapper = $('.form-wrapper');
                var errorMessageSection = $("<section class=\"email-error\"></section>");
                formWrapper.empty().append(errorMessageSection);
                errorMessageSection.html("<p>Sorry, there was an error.</p><p>Request error type: " + request + "</p><p>Error type: " + errorType + "</p><p>Error Message: " + errorMessage + "</p>").hide().fadeIn(1500);  
            },
            timeout:5000,
            beforeSend: function() {
                //add spinner
                $('.form-wrapper').empty().addClass('is-loading');
            },
            complete: function() {
                //remove spinner
                $('.form-wrapper').removeClass('is-loading');
            }
        });

At the top of my processing file I have:

$sentMail = false;

And then it runs through my validation and then at the end:

$sentMail = $mail->Send();
    if(!$sentMail) {
        $errors['mailNotSent'] = true;
    }
     if($sentMail) {
                header('Location: thankyou-nojs.php');
                exit;
   }
}
2
  • 1
    Way too many question in one post. Focus on one thing and ask. Post multiple questions if needed. However, you'll need to show what you've tried. Q1 = becasue mail() returns TRUE or FASLE Commented Mar 5, 2014 at 1:34
  • Many apologies, I realize it was quite long after reading it over. So the 'success' in this case is based on what mail() returns? Commented Mar 5, 2014 at 1:42

2 Answers 2

1

Regarding the first part, SMTP or Email isn't a guaranteed delivery mechanism. Your mail function will probably return OK if your local smtp sending server just accepted the mail , i.e. there were no syntax issues, email address looks right etc. But from there it's out of your hands and in the SMTP servers hands to do all the work of trying to send the mail to the place you wanted to send it. So it's a fire and forget operation with SMTP unfortunately. It's been around so long that we just assume it will work for most cases. If the target recipient's SMTP server is down for example, the mail server will retry again later etc.

It's a broad question with a broad answer, but I hope that helps.

Regarding the second part of the question:

The ajax request doesn't follow redirects in the headers because it's not part of the normal flow of the user's browser, it's meant for background actions, so you would need to explicitly redirect the user's browser in JavaScript with that header if you meant to.. Whereas redirect headers in pages requested via "normal" means or the browsers normal flow will interpret that header.

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

3 Comments

Thank you so much David, this was actually very good. What you're saying is in this case, the 'success' is based on if mail() returns true or false?
Also, thanks, I see that it probably passes over the header() bit but ignores (doesn't follow it). Is that safe to say?
Yeah that's safe to say. You can check exactly what headers are passed between your browser and the server if you press F12 and go to the Network tab that pops up.
1

how does Ajax determine the mail was sent successfully?

it doesnt.

ajax returns successful because it posted to the target file and processed it without error. whether the mail went anywhere or not is undetermined by ajax. what i do is:

on the target page:

if (mail(blah..blah)
{
  echo "success"; 
}
else  
{
   echo "fail";
 }

and in the ajax:

...
success: function(result){
if (result == "success")
{
   alert("mail was sent");
}
if (result == "fail")
{
   alert("mail did not get sent");
}

this doesn't guarantee mail made it. just that it was attempted to be sent.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.