I have a jquery function for sending email with ajax request;
function sendMail(from,to,subject,message){
var datastr="from="+from+"&to="+to+"&subject="+subject+"&message="+message;
$.ajax({
type: "POST",
url: "mail.php",
data: datastr,
cache: false,
success: function(html){
return true;
},
error: function(jqXHR,status,error){
return false;
}
});
}
now I want to tell the user if the mail was successfully sent or not like this:
$("#feedback").html("Sending email...");
if(sendMail("from@email.com","to@email.com","MySubject","MyMessage"))
$("#feedback").html("Email sent.");
else
$("#feedback").html("Error sending email.");
but of course jQuery processes the if condition before the mail was sent, so the condition is false :-( How do I tell jQuery to wait until sendMail has completed and returns something?