May 17, 2013 not receiving errors from php script

Preahkumpii’s Question:

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 function to correctly receive the error from a php script. Here are both.


$toemail = "email here";

# SUBJECT (Subscribe/Remove)
$subject = "Someone has contacted International Designs";

$location = "../thank-you.php";

$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 . ">";

    $body .= "Name: " . $myname . " nn";
    $body .= "Email: " . $myemail . " nn";
    $body .= "Message: " . $mymessage . " nn";
    # add more fields here if required

    ## SEND MESSGAE ##

    mail( $toemail, $subject, $body, "From: $email" ) or die ("Mail could not be sent.");




if (verify(myname, myemail, mymessage, human, hash, patt)) {
  $.post(myform.attr('action'), myform.serialize(), function() {
    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.

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!"));

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!