0

I am struggling on an AJAX call I make to let me read out the return value. My php code returns automatically an echo with false or true, which I made to check if the mail was send. But I am struggling to read out the value and get it to work.

Here is my AJAX code

if(proceed == true){            
    $.ajax({
        type    : 'POST',
        url     : 'inc/actions/sendmail.php',
        data    : $('#sentMessage').serialize(),
        cache   : false,
        dataType: 'text',
        success : function (data) 
        { 
            if (data == 'true'){
                $("#sentMessage")[0].reset();
                $("#err").append("<div class='alert alert-success' role='alert'>Bedankt voor je bericht, wij nemen binnenkort contact met je op.</div>");
                $('input[name=name]').css('border-color','')
                $('input[name=email]').css('border-color','')
                $('textarea[name=message]').css('border-color','')
                setTimeout(function(){$('#err').fadeOut();}, 5000);
            }else{
                $("#err").append("<div class='alert alert-danger' role='alert'>Je telefoonnummer bestaat niet uit 10 cijfers.</div>");
                $('input[name=phone]').css({'border':'2px solid red'});
                setTimeout(function(){$('#err').fadeOut();}, 5000); 
            }
        },
        error   : function (jqXHR, textStatus, errorThrown) {console.log(errorThrown);}
    }); 
}

As you figure, it will push out the else statement constantly, cause it doesn't pass the if statement, since it doesn't know what to check.

Huangism
  • 16,278
  • 7
  • 48
  • 74
Dorvalla
  • 5,027
  • 4
  • 28
  • 45
  • 3
    Add console.log(data); right before your conditional. Then inspect the results of your data object. Chances are it's not just a simple boolean. If it is a boolean, then try changing your conditional to if(data) instead of if(data == 'true'). – Mike Pugh Sep 03 '14 at 15:45
  • Use `console.log( data );` as the first statement inside the `success` handler so you can see what's being returned. May be you may need to use `if( data.trim() == 'true' ) ...` – PeterKA Sep 03 '14 at 15:46
  • May be this question/answer can help you: http://stackoverflow.com/questions/4948663/php-get-bool-to-echo-false-when-false – Mindastic Sep 03 '14 at 15:47
  • 1
    actually, i use an echo within the php, not a boolean. Thansk for the tip though on console, slipped my mind. It does indeed tell me `true` as what is returned. – Dorvalla Sep 03 '14 at 15:49
  • You don't show what the actual output of your PHP script is. Whatever the case, make sure you use `json_encode()`. – Brad Sep 03 '14 at 15:51
  • 1
    @Brad Just to know: Why need `json_encode` when only echoing a single word? – sri Sep 03 '14 at 15:58
  • 1
    @sri You technically don't, but inevitably these things grow. For instance, what happens when the call fails? Maybe you want to tell the user why it failed, and a simple boolean isn't enough. – Brad Sep 03 '14 at 16:11

2 Answers2

2

Even though all you're outputting is true/false from your PHP script, and even after you've confirmed that console.log( data ) outputs true/false, there may be white spaces around the string. Therefore you may want to use:

if( data.trim() == 'true' ) { ....

if( data.indexOf('true') > -1 ) { ..... //<=== OR

Just to confirm, you may want to use:

console.log( "'" + data + "'" );
PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • That did the trick, I think indeed there was a whitespace involved. I will check the php file for it. – Dorvalla Sep 03 '14 at 15:58
0

You should try console.log(data) to check exactly what your server is returning. There is also a change it is an object that is being returned, so you may need console.log(JSON.stringify(data)).

Also, this probably is not related but you should always be using triple equals (===), it is good practice in JavaScript.

catalyst294
  • 129
  • 9