3

Problem I have.... I have listener script in php that does this:

  if ($count != 1) {echo 'no';} else { echo "yes";}

So it echoes "yes" or "no" depending if task was success or not and on my page I have this:

   jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function (r) {
    if (r) $.post('includes/publishers/delete-publisher.php?publisherid=' + publisherid, 
    function(data) {
            if (data == 'no') {
            $.jGrowl('Error - PUBLISHER WAS ALREDY DELETED !');
            alert("Data Loaded: " + data);
            } else {
            $(element).parents('tr').remove();
            $.jGrowl('Publisher deleted');       
            alert("Data Loaded: " + data);
            }
        });
});

And the problem is that ALTHOUGH delete-publisher.php echoes "no" (I see it echoed in alert box) - JQuery is always processing this as if response was "yes" !? Am I missing something obvious ?

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
Peter
  • 1,264
  • 5
  • 20
  • 41

2 Answers2

2

Save yourself a lot of headaches and use JSON to send responses back to your javascript. do something like this (untested):

PHP Side:

header('Content-type: application/json');
if ($count != 1) {
    echo json_encode(array('success'=>false));
} else { 
    echo json_encode(array('success'=>true))
}

JS Side:

jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function (r) {
if (r) $.post('includes/publishers/delete-publisher.php?publisherid=' + publisherid, 
function(data) {
        if (data.success == false) {
            $.jGrowl('Error - PUBLISHER WAS ALREDY DELETED !');
            alert("Data Loaded: " + data);
        } else {
            $(element).parents('tr').remove();
            $.jGrowl('Publisher deleted');       
            alert("Data Loaded: " + data);
        }
    }, 'json');

});

This way you can pass error messages back to the JS too.

You should also get used to using something like Firebug for Firefox so you can look into the details of the network traffic, and post parameters, response headers, etc, set JS breakpoints to help debug.

Levi
  • 2,103
  • 14
  • 9
1

Likely there's some whitespace in your response. Try using this instead:

if ($.trim(data) == 'no') {
Travesty3
  • 14,351
  • 6
  • 61
  • 98
  • 1
    Thats a hack, he should know what comes in it exactly. – fonZ Sep 21 '12 at 19:33
  • 1
    Call it a hack if you want, but it makes the app more robust. I don't see a problem. A problem as simple as extra whitespace after the closing `?>` tag will cause a problem. This solution still works for that scenario. – Travesty3 Sep 21 '12 at 19:35
  • YES, correct it was response with line break yes and line break no .... not sure why, but anyway - this FIXED IT ! thanks !!!! – Peter Sep 21 '12 at 19:36
  • And more prone for errors, because its not exact. It should not happen, when you send something you should get exactly what you sent or you sent it wrong. – fonZ Sep 21 '12 at 19:37
  • this is plain script that has 2 solutions YES and NO,so I don't see a problem there ? What is than a "correct solution" ? – Peter Sep 21 '12 at 19:39
  • A good solution to this type of problem is to use JSON rather than raw data. – Barmar Sep 21 '12 at 19:40
  • Well if there is a linebreak you should check for 'no\n' and not trim or anything like that because that first takes more unneeded resources and second is not exact, means multiple outcomes possible. – fonZ Sep 21 '12 at 19:41
  • yes, there was empty line between opened and closed tags... :( THANSK ALL AND GOOD POINT ABOUT JSON - for more complex stuff that shold be used, but this is like yes-no situation :) THANKS ALL ! – Peter Sep 21 '12 at 19:43