Nick Law
Nick Law

Reputation: 1748

jQuery AJAX $.post not working with .click() method

Having a little difficulty with my ajax request. It seems that the $.post method is simply not working, no request is being sent. Nothing is showing in firebug either.

I can get this to work:

   $('.comment-remove').click(function (evt) {

        evt.preventDefault();
        var sure = confirm('Are you sure your want to remove your comment from this thread?');

        if(sure) {              
            var comment_id = $(this).attr('id');
            alert(member_id);
        }
    });

but not this:

   $('.comment-remove').click(function (evt) {

        evt.preventDefault();
        var sure = confirm('Are you sure your want to remove your comment from this thread?');

        if(sure) {
            var comment_id = $(this).attr('id');

            $.post('comment.php', { comment_id: comment_id }, function(data) {
                if (data === 'success') {
                    alert(data);
                } else {
                    alert('Unable to remove comment at this time.');
                }
            }
        }
    });

And HTML is:

<a class="comment-remove" id="<?php echo $comment_id; ?>">Remove my comment</a>

Upvotes: 0

Views: 70

Answers (2)

devonnigelreed
devonnigelreed

Reputation: 19

Your post call is missing a closing parenthesis. Try this:

    $('.comment-remove').click(function (evt) {

        evt.preventDefault();
        var sure = confirm('Are you sure your want to remove your comment from this thread?');

        if(sure) {
            var comment_id = $(this).attr('id');

            $.post('comment.php', { comment_id: comment_id }, function(data) {
                if (data === 'success') {
                    alert(data);
                } else {
                    alert('Unable to remove comment at this time.');
                }
            })
        }
    });

Upvotes: 1

Niall Paterson
Niall Paterson

Reputation: 3580

It should be:

$.post('comment.php', { comment_id: comment_id }, function(data) {
  if (data === 'success') {
    alert(data);
  } else {
    alert('Unable to remove comment at this time.');
  }
})

See the extra ) I added? It matches the ( in $.post( at the start. You were missing it, it's always easy to do. Your console should be able to tell you about this kind of thing.

Upvotes: 7

Related Questions