Reputation: 1301
I know this has been asked before and I have looked at every post I could find that deals with this. I still cannot get the jQuery.post function to correctly receive the error from a php script. Here are both.
PHP:
<?php
##CONFIGURATION
# LIST EMAIL ADDRESS
$toemail = "email here";
# SUBJECT (Subscribe/Remove)
$subject = "Someone has contacted International Designs";
# RESULT PAGE
$location = "../thank-you.php";
## FORM VALUES ##
$myname = $_REQUEST['myname'];
$myemail = $_REQUEST['myemail'];
$mymessage = $_REQUEST['mymessage'];
if ( empty($myname) || empty($myemail) || empty($mymessage) ) {
exit('{"error":"That value was invalid!"}')
} else {
# SENDER
$email = $myname . " <" . $myemail . ">";
# MAIL BODY
$body .= "Name: " . $myname . " \n\n";
$body .= "Email: " . $myemail . " \n\n";
$body .= "Message: " . $mymessage . " \n\n";
# add more fields here if required
## SEND MESSGAE ##
mail( $toemail, $subject, $body, "From: $email" ) or die ("Mail could not be sent.");
}
?>
JS:
if (verify(myname, myemail, mymessage, human, hash, patt)) {
$.post(myform.attr('action'), myform.serialize(), function() {
$('#email-success').fadeIn();
myform[0].reset();
setTimeout("$('#email-success').fadeOut();", 5000);
}, 'json')
.fail(function() {
alert('An error has occurred. Please try again later.')
});
}
I have tried about 5 different methods already, none of which have worked. When I put 'json' as the datatype in the .post function the .fail always fires, no matter what's in the php script. If I leave datatype out, then .fail never fires under any circumstance. I have a feeling the problem is with the php commands and datatype. Any help is appreciated.
Upvotes: 0
Views: 384
Reputation: 717
Perhaps it's because you don't change the http header code of your response and don't specify your data type response.
You're php code response to front (jQuery) code with a "200 – OK" status in any case, but you expect an error in some case with an 'HTTP/1.0 400 – Bad Request' response Or a 'HTTP/1.0 500 Internal Server Error'.
And, like say Eggplant, you have to specify your response data type as 'Content-Type: application/json'.
So, the final code would be something like this :
<?php
...
header('HTTP/1.0 204 – No Content', true, 204);
header('Content-Type: application/json');
if ( empty($myname) || empty($myemail) || empty($mymessage) ) {
header('HTTP/1.0 400 – Bad Request', true, 400);
exit('{"error":"That value was invalid!"}')
} else {
...
$send = mail( $toemail, $subject, $body, "From: $email" );
if (!$send)
header('HTTP/1.0 500 – Internal Server Error', true, 500);
exit ('{"error":"Mail could not be sent."}');
}
}
return;
?>
For the Warning Cannot modify header information - headers already sent by (output started at /homepages/.../design/contact.php:1)
you can examine this answer on the same problem.
Output can be:
Update after chat session : it's was a UTF-8 BOM problem
Upvotes: 2
Reputation: 78991
Although, yours is a valid JSON data, I always recommend to use json_encode() function to create JSON string.
exit(json_encode(array("error" => "That value was invalid!")));
Another is make sure you send correct headers to ensure the script knows its a json data. So
header('Content-Type: application/json');
exit(json_encode("error" => "That value was invalid!"));
Upvotes: 0
Reputation: 1310
try this
if (verify(myname, myemail, mymessage, human, hash, patt)) {
$.ajax({
type :'POST',
data :$("#myformid").serialize(),
beforeSend:function(){
},
success:function(res){
if(res=='ok'){
setTimeout("$('#email-success').fadeOut();", 5000);
}else{
//read respon from server
alert(res)
}
},
error:function(){
//error handler
}
});
}
and just example
$send = mail(....);//your mail function here
echo ($send) ? "ok" : "Error";
Upvotes: 0