slj90 Posted February 26, 2015 Share Posted February 26, 2015 I don't understand why these if statements aren't working: $.post('./action/forgotpasswordcheck.php',{username:tu, email:te},function(f){ if(f=='correct') { alert('Correct!') } else if(f=='incorrect') { alert('Incorrect!') } else { alert(f) } }); } It alerts the last alert, alert(f) and it either says 'correct' or 'incorrect' so why aren't the if statements working? forgotpasswordcheck.php contains: if ($rowcount == 0) { echo "incorrect"; } else { echo "correct"; } Thanks in advance, Quote Link to comment https://forums.phpfreaks.com/topic/294923-alertf-alerts-correct-but-iffcorrect-doesnt-work/ Share on other sites More sharing options...
maxxd Posted February 26, 2015 Share Posted February 26, 2015 In your php file, echo a json-encoded array and check those values in your javascript. Along the lines of: ... echo json_encode(array('status'=>'correct')); ... and in your JS: if(f.status == 'correct'){ ... Quote Link to comment https://forums.phpfreaks.com/topic/294923-alertf-alerts-correct-but-iffcorrect-doesnt-work/#findComment-1506873 Share on other sites More sharing options...
slj90 Posted February 26, 2015 Author Share Posted February 26, 2015 Hi, thanks for your reply, I've made the changes but now none of the javascript on the page is working. $.post('./action/forgotpasswordcheck.php',{username:tu, email:te},function(f){ if(f.status == 'correct'){ alert('Correct!') if(f.status == 'incorrect'){ alert('Incorrect!') } else { alert(f) } }); } Please advise, Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/294923-alertf-alerts-correct-but-iffcorrect-doesnt-work/#findComment-1506876 Share on other sites More sharing options...
slj90 Posted February 26, 2015 Author Share Posted February 26, 2015 It's working if I see if the string contains "correct": if (f.indexOf("correct") >= 0) { alert('Correct!') } Quote Link to comment https://forums.phpfreaks.com/topic/294923-alertf-alerts-correct-but-iffcorrect-doesnt-work/#findComment-1506878 Share on other sites More sharing options...
maxxd Posted March 2, 2015 Share Posted March 2, 2015 Did you get this working? If not, print the contents of f to your console and check it there. If the status isn't 'correct', but for instance, "you are correct, sir or ma'am!", then my code wont' work because it's checking equality. However, your code would work because the word 'correct' is present in the string somewhere after the first letter of the string. It's also possible that the JSON output from forgotpasswordcheck.php isn't being parsed in JavaScript before the check is made. Quote Link to comment https://forums.phpfreaks.com/topic/294923-alertf-alerts-correct-but-iffcorrect-doesnt-work/#findComment-1507265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.