0

Hello guys this is pretty embarrassing. I am returning a string variable from php to javascript. On there I want to make a simple comparison and take action accordingly but the results are not as expected.

First I make a post to a form my javascript file to php for processing.

$('#matchForm3').submit(function (event) {
    var pr = $('#cprovider3').val();
    var amount = $('#camount3').val();
    var ctc = $('#ctc3').val();

    var formData = {'cprovider': pr, 'camount': amount, 'ctc': ctc, 'creceiver': rc3, 'ramount': ramount3};

    $.ajax({
        url: '/backend/matchForm.php',
        type: 'post',
        data: formData,
        success: matchResponce,
        error: function (xhr, desc, err) {
            alert(xhr);
            return false;
        }
    }); 

    return false;

});

function matchResponce(data, status) {
    //alert(data);
    if (data != 'legit') {
        //do something
    }else {
        //do something else
    }
}

And then in the matchForm.php file I do some processing and return any of two possible values "legit" or "no no".

//do something up here

if(value == true){
     echo 'legit';
}else{
     echo 'no no';
}

Now back in the script.js file in the callback method matchResponce(). I want to make that check and act accordingly but it meets the first condition every time, even though the data is different. Thanks in advance!!

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
Kenneth Ngedo
  • 75
  • 1
  • 13
  • 2
    dump the data in js function and see the returned values – Shady Atef Feb 17 '17 at 18:25
  • if(value) already does it. Also use === and !== for comparing strings. Is there any space outside of the php code? (ive had that mistake often) – Jonas Wilms Feb 17 '17 at 19:15
  • To see the data returned in JavaScript, try `console.log(data);` instead of `alert()` which transforms the message to text. It will show you whether you got text (a string), JSON, XML, or whatever you received like that. – Alexis Wilke Feb 17 '17 at 19:22
  • It's difficult to answer without seeing what you're doing with your php script. – zsawaf Feb 17 '17 at 19:51
  • Here is explanation. http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Blarz Feb 17 '17 at 19:59

2 Answers2

0

I suggest not returning a string value, but an integer instead. string values are prone to user input mistakes, more so than a number. Try,

if($myProcess) {
    echo 1;
} else {
    echo 0;
}

as your return statement in matchForm.php . This will check if your process evaluates to 'true' and echoes '1' or 'false' and echoes '0'. From here,

function matchResponce(data, status) {
    //alert(data);
    if (data == 1) {
    //do something
    }else {
    //do something else
    }
}

checks the return and advances onward.

bowl0stu
  • 352
  • 1
  • 12
  • Tried that, but it still did not work as expected. On each case the first condition is met, so I it only "does something". It never "does something else" – Kenneth Ngedo Feb 18 '17 at 07:03
  • sounds like your code is messed up somewhere. can you force it to fail? – bowl0stu Feb 20 '17 at 13:02
0

I fixed it now, all I had to do was to convert the variable data to integer first, before making the comparison against integer constants, this is to ensure that both of the operands are off the same integer type. Now this is what callback function matchResponce() looks like.

function matchResponce(data, status) {

    var h = parseInt(data);
    //alert(h);
    if (data == 0) {
        //do something
    } else (data == 2) {
       //do something else
    } 

}
Kenneth Ngedo
  • 75
  • 1
  • 13