0

I have a simple voting mechanism Im trying to block out. I got some help earlier and it works on its own but I'm trying to do it with an ajax callback - i get the returned data but the funciton isn't firing for some reason (although it works with a simple alert as the callback function) but it's not incrementing the numbers. Here's the simplified HTML

<form name="vote" id="vote" method="post" action="PHP/success.php">
    <a class="vote">Good</a><input class="qty" type="text" name="qty" value="0" readonly="readonly" />
    <a class="vote">Bad</a><input class="qty" type="text" name="qty" value="0" readonly="readonly" />
</form>

Here is the dummy PHP (PHP/success.php) that only performs an echo at this point

<?php
    echo('ok');
?>

and my click function

$(".vote").click(function() {
    $.ajax({
        type: "POST",
        url: "PHP/success.php",
        success: function(data) {
            if (data=="ok") {
                var input = $(this).next(".qty");
                input.val(parseFloat(input.val()) + 1);
            } else {
        alert('error');
            }
        }
    });
});

again if i replace the (data="ok") var and function with an alert it works fine. thx

5
  • Could just be whitespace on data, try $.trim(data) == 'ok' instead. Commented Nov 2, 2011 at 4:00
  • What happens if i click Bad? $(this).next(".qty") will fail in that case. Commented Nov 2, 2011 at 4:02
  • @muistooshort nope. this is puzzling...it looks right doesn't it? Commented Nov 2, 2011 at 4:03
  • @jSang it seems to work when it's not in the context of an ajax call dirtybirddesignlab.com/Wronglish/test.html Commented Nov 2, 2011 at 4:04
  • Sorry, i missed the input tag, my bad. BTW could you show us the exact value of the ajax call data? Commented Nov 2, 2011 at 4:08

2 Answers 2

4

i think problem is with this line.

var input = $(this).next(".qty");

$(this) is not giving the clicked element there.

if you replace it by

var input = $(".vote").next(".qty");

the function works.

EDIT:

I tried with this and it worked.

 $(".vote").click(function() {
                      var kk = $(this);

$.ajax({
    type: "POST",
    url: "a.php",
    success: function(data) {

    if (data=="ok") {

            var input = kk.next(".qty");
            input.val(parseFloat(input.val()) + 1);
        } else {
    alert('error');
        }


    }
});
});
Sign up to request clarification or add additional context in comments.

3 Comments

That seems right, but you should probably save a reference to this outside the callback instead - your answer won't work correctly if there are multiple .vote elements.
@nrabinowitz so just move the var kk = $(this) up above the .ajax call, similar to xdazz's answer right? that would make it global correct?
@DirtyBirdDesign - yup, that's what I was thinking.
2

Put var input = $(this).next(".qty"); outside.

$(".vote").click(function() {
    var input = $(this).next(".qty");
    $.ajax({
        type: "POST",
        url: "PHP/success.php",
        success: function(data) {
            if (data=="ok") {            
                input.val(parseFloat(input.val()) + 1);
            } else {
        alert('error');
            }
        }
    });
});

1 Comment

awesome. always had issues with the "think globally act locally" stuff. I appreciate it man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.