views:

49

answers:

3

i have this jquery code with an ajax request that works fine, but the jquery is not displaying the results(vote_count) and changing the upvote image, just like stackoverflow:

jquery code:

$(function(){
    $("a.vote_up").click(function(){
    //get the id
    the_id = $(this).attr('id');

    //the main ajax request
    $.ajax({
        type: "POST",
        data: "action=vote_up&id="+$(this).attr("id"),
        url: "ajax/votes.php",
        success: function(msg)
        {
            //echo the votes count
            $("span#votes_count"+the_id).html(msg);
            //replace the new vote count
            $("span#votes_count"+the_id).fadeIn();
            //replace the image with active arrow
            $("#vote_up"+the_id).attr("src", "img/upvoteActive.png");

        }
    });
});

the html code:

<li class ="record">
<span class="vote_count">$net_vote</span> 

  <a href='#' class='vote_up' id=$id><img src="img/uparrow.png"></a>

  <a href='#' class='vote_down' id=$id><img src="img/downarrow.png"></a>
</li>

to clarify everything again, the ajax request is fine its upvoting the right answer, the problem is in the success bit of the ajax, the new vote count is not showing, and the image is not being replaced to an active arrow(just like stack overflower) thanks :))

+1  A: 

In your jQuery code you use #votes_count but the html does not have an id but a vote_count class (without the s).

So it should read span.vote_count

same with the upvote, you use an id to target it but it has a class..

Gaby
`vote_count` is a class, not an id at all!
thenduks
so whats the solution, i dnt really get what you mean!! sorry
getaway
A: 

You were incorrectly targetting the spans, and also, sending get parameters over a post request. I have fixed below:

 $(function(){
        $("a.vote_up").click(function(){
        //get the id
        the_id = $(this).attr('id');

    //the main ajax request
            $.ajax({
                type: "POST",
                data: {'action' : 'vote_up','id' : $(this).attr("id")} // here
                url: "ajax/votes.php",
                success: function(msg)
                {
                                    //echo the votes count
                    $("span.vote_count").html(msg); //here
                    //replace the new vote count
                    $("span.vote_count").fadeIn(); // here
                    //replace the image with active arrow
                               $(".vote_up").find(the_id).attr("src", "img/upvoteActive.png"); // and here

                }
            });
        });
Liam Bailey
That's not it, jQuery serializes an object passed as data into what you see in his post anyway. jQuery also knows where to put params in POST vs GET requests. Also, you forgot a comma at the end of the `data: ...` line :)
thenduks
The main AJAX call does, but .post doesn't
Liam Bailey
like i have stated before there is nothing wrong with the ajax request, its the jquery fade.id and changing the image after the success call that is the problem :))
getaway
As you can see I have corrected the code in full in an edit
Liam Bailey
+2  A: 

Your selectors in the success callback are all wrong. You have <span class="vote_count">...</span> but then try to update it's value as if it had an id: $("span#votes_count"+the_id)...

You probably want your code more like:

success: function(msg) {
  //echo the votes count
  $("span.vote_count#"+the_id).html(msg);
  //replace the new vote count
  $("span.vote_count#"+the_id).fadeIn();
  //replace the image with active arrow
  $(".vote_up#"+the_id).attr("src", "img/upvoteActive.png");
}

So as you can see, you'll also want to add the id of the object into the .vote_count element as well.

Update:

To be clearer, here is the updated markup:

<span class="vote_count" id="$id">$net_vote</span> 
<a href='#' class='vote_up' id="$id"><img src="img/uparrow.png" /></a>
<a href='#' class='vote_down' id="$id"><img src="img/downarrow.png" /></a>

And here is the updated javascript:

$(document).ready( function() {
  $("a.vote_up").click(function(){
    //get the id
    var the_id = $(this).attr('id');

    //the main ajax request
    $.ajax( {
      type: "POST",
      data: "action=vote_up&id=" + the_id,
      url: "ajax/votes.php",
      success: function( msg ) {
        $("span.vote_count#"+the_id).html(msg).fadeIn();
        $(".vote_up#" + the_id + " img").attr("src", "img/upvoteActive.png");
      }
    } );
  } );
} );
thenduks
i have changed it but still not working!! :((
getaway
I updated my answer.
thenduks
thanks the the vote_count is working hooooray thank you , +upvote from me!! but the image is still not changing
getaway
Ah ha, missed that. Check newest edit. You weren't selecting the actual image that's within the anchor.
thenduks
loool, its still not working, i dont why it wnt change!! hahaha
getaway
Then your image must not exist? Use firebug... does the `img` element's `src` change at least?
thenduks