0

Here is my if statement:

AND a Fiddle: http://jsfiddle.net/setMa/

$('#addamenity').click(function () {
    var id = $('.editblock').find('#id').val();
    var amenity = $("#amenity").val();

    var dataString = {
        id: id,
        amenity: amenity
    };
    console.log(dataString);
    if (amenity != '' || id != '') {
        $.ajax({
            type: "POST",
            url: "classes/add_amenities.php",
            data: dataString,
            cache: false,
            async: false,
            success: function (html) {
                $('.editunitamenities').load('classes/display_amenities.php?id=' + id);
            }
        });
    } else {
        alert('You must SAVE your changes to the new unit before adding amenities.');
    }
});

Now, my console log clearly shows an id of "" what gives? If I take the || off the if or leave the text field blank it works just fine.

nbrooks
  • 18,126
  • 5
  • 54
  • 66
Burning Hippo
  • 787
  • 1
  • 20
  • 47
  • I'm not sure what you're saying. Is the value of `$('.editblock').find('#id').val()` the empty string? Often it's useful to use a service such as [JSFiddle](http://jsfiddle.net) so that others are able to see your problem too. – Qantas 94 Heavy Nov 17 '13 at 04:45
  • @Qantas 94 Heavy Added the Fiddle. @bfavaretto No, because the `#amenity` might not be blank. – Burning Hippo Nov 17 '13 at 04:50
  • So you want to enter the if except when both values are blank? – bfavaretto Nov 17 '13 at 04:54
  • I fixed it, you can read my answer to understand better what I was wanting – Burning Hippo Nov 17 '13 at 04:59
  • Yeah, in `amenity != "" || foo` , the value of `foo` is completely ignored if amenity is set. – Kent Fredric Nov 17 '13 at 05:08
  • What I see: `[ amenity = "value", id = "" ]` → no error occurs. `[ amenity = "", id = "" ]` → error occurs. `ID` is hard-coded to be `""`. What other behaviour are you expecting? Its entirely not obvious. – Kent Fredric Nov 17 '13 at 05:22

4 Answers4

1

You're logging it before you check it. Change

var dataString ={id:id,amenity:amenity};
console.log(dataString);    
if (amenity != '' || id != '')
{

to

if (amenity != '' && id != '')
{
    var dataString ={id:id,amenity:amenity};
    console.log(dataString);    

and you want AND (not OR), because otherwise your dataString will contain blanks (whenever one is blank and the other isn't).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

The logical OR operator returns true as long as at least one of the conditions is true. This means that, in your case, the if statement will always be executed if either one of the values is not blank. This means that even if id is blank, if amenity is not blank, the statement passes--that's what OR accomplishes.

In fact, because the logical OR operator is short-circuiting, it doesn't even check the value of id: in the expression (amenity != '' || id != ''), the left-side is evaluated. If it evaluates to true, the entire statement becomes true, and the if block is evaluated; the right-side is ignored.

If amenity != '' evaluates to false, the OR operator simply returns id != ''. Since the left-side is false, the right-side alone will determine the value of the entire expression, so that's what is returned. If it is true, then at least one condition is true, and the if block is evaluated. If it is false, they both are, and the if block is skipped.

The solution, if you want really want your code to be evaluated only for a non-blank ID, is to only check the id: if (id != ''). In fact, in the JavaScript world of truthiness, you can simply say if (id), and that's all you need.

if (id) {
    // do ajax post
}
else {
    // log errors
}
nbrooks
  • 18,126
  • 5
  • 54
  • 66
1

From your question I really can't tell what you're looking for. It sounds like you're frustrated with the evaluation of your conditional test. The logical "or" operator means "this statement is true if any of its terms are true". So you're checking if "amenity", "id", or both of them are not "loosely equal to" the empty string. That means if amenity, id, or both of them are assigned then whatever is in your "if" statement block will be executed. There's a little example code below to show you but, again, it's difficult to tell what it is you're after.

function aTest (amenity, id) {
    aTest.count += 1;
    console.log('### Test # ', aTest.count);
    console.log('amenity = ', amenity);
    console.log('id = ', id);
    if (amenity != '' || id != '') {
        console.log('amenity, id, or both: ' +
                    'is not kinda equal to an empty string');
    }
}
aTest.count = 0;

aTest('', '');
aTest('', 'x');
aTest('x', '');
aTest('x', 'x');
Kastor
  • 617
  • 5
  • 14
-1

Fixed it.

Not sure why everyone is suggesting AND when I want OR. If I only wanted it to fail when both were blank I would've used &&. I know the difference...?

I want the .ajax() to not run regardless of the value of #amenity. I could not figure out why it wasn't working. Instead, since I don't care what is in #amenity I decided why check it? I took that check out and all works fine.

if (id != '')
    {
        var dataString ={id:id,amenity:amenity};
        console.log(dataString);    
        $.ajax({        
            type: "POST",
            url: "classes/add_amenities.php",
            data: dataString,
            cache: false,
            async:false,
            success: function(html)
            {
                //$('.editunitamenities').html(html);
                $('.editunitamenities').load('classes/display_amenities.php?id='+id);
            }
        });
        //$('.editunitamenities').load('.editunitamenities');
    }else { alert('You must SAVE your changes to the new unit before adding amenities.'); }
Burning Hippo
  • 787
  • 1
  • 20
  • 47
  • 1
    "I want the .ajax() to not run regardless of the value of #amenity" In your Fiddle, the "Id" field is blank, and there is no way to set it. So .... – Kent Fredric Nov 17 '13 at 05:24