I try to confirm that an email was sent with JSON and then cath the event with ajax and display a succes message on the screen.
The problem is that the email is not sent, and I get redirected to the “sended”:true because of the first part of the php script. The html and css is ok, I have tested it a lot.
What I actually want is to send the email with php and than use ajax to display a succes or error message. The problem is that I this is the first time that I use php and I just can’t find the correct way. I would not ask this, but I have to deliver the project by tonight and this is the last thing to be done.
You can check it in action at: http://webofdreams.ro/vworker/finemaid/finemaid.html# under email us link.
JS:
$ajax({
"type":"POST",
"url":"sendemail1.php",
"data": { name1: name1Val, emailFrom1: emailFrom1Val, comments: commentsVal},
"dataType":'json',
"success":function(response){
if (response.sended){
alert ("Mail Sended ok"); //Code after mail send
}else{
alert (response.error); //Code or allert on error
}
},
error: function (xhr, ajaxOptions, thrownError){
alert(xhr.status+" "+thrownError);
}
php:
$send = @mail($mailTo, $subject, $message, "From: ".$mailFrom1);
if ($send){
echo mail($mailTo, $subject, $message, "From: ".$mailFrom1) ? '{"sended":true}':'{"sended":false,"error":"Mail send fail."}';
}else{
echo '{"sended":false,"error":"Request Error."}';
};
Your ajax request is full of errors so i have wrote it from scratch, the below script will atleast take care of the ajax request.