-4

Just created a simple script trying to make a conditional statement but I don't know why and what is wring that I am getting his error in my console

Uncaught ReferenceError: Invalid left-hand side in assignment

Here is my code please let me know what is wrong with this

$(document).ready(function() {
$("#postcomment").click(function() {
    var name    = $('#name').val();
    var email   = $('#email').val();
    var comment = $('#comment').val();
    var post    = $('#pt').val();
    if(email == null && name == null && comment = null) {
        alert('Fields cannot be empty');
    } else {
        $.ajax({
            type   : "POST",
            url    : "include/get_data.php",
            dataType : "text",
            data   : {name : name, email : email, comment : comment, post : post, command : 'comment'},
            success  : function(data) {
                $("#all").apppend(data.commenting);
                $("#counts").html(data.count);
                document.getElementById('name').value = '';
                document.getElementById('email').value = '';
                document.getElementById('comment').value = '';
            }
        });
    }
});
});
Mark Alan
  • 435
  • 1
  • 5
  • 19
  • 3
    Invalid here `comment = null`. Should be `comment == null` – j08691 Mar 08 '17 at 20:57
  • 2
    Try `comment == null`. – ventiseis Mar 08 '17 at 20:58
  • Honestly, just search for the error message. The duplicate was the first result on google.com when searching for "Invalid left-hand side in assignment"... – Heretic Monkey Mar 08 '17 at 21:04
  • The issue here is that `comment = null` is an assignment, but that mistake doesn't cause an error by itself. What actually causes that error is that you're using an assignment alongside a logical `&&`, which causes the line to be read like `(something && comment) = null`, but the result of the `&&` operator is not a valid reference to use in an assignment. – apsillers Mar 08 '17 at 21:08

2 Answers2

1

Your if statement has comment = null which is an assignment operator. Meaning you're accidentally setting comment to null when you do that. To do comparisons you need == or === equality operators.

if(email == null && name == null && comment = null) {

Should be

if(email == null && name == null && comment == null) {
Soviut
  • 88,194
  • 49
  • 192
  • 260
-2

In your if you have comment = null, change it to ==.

user7491506
  • 186
  • 1
  • 3
  • 1
    This was noted twice in the comments above. Instead of trying to farm rep from the typo, please vote to close the question as off-topic. – j08691 Mar 08 '17 at 21:01