I have a voting system that sends the identifier of a clicked element to a PHP script, PHP updates the database and echoes back the new vote count through a JSON encoded array.
This is jQuery:
$(".vote_up").click(function(){ var id = this.id; var vote = $(this).attr("class"); var data = "id=" + id + "&vote=" + vote; $.ajax ({ type: "POST", url: "vote.php", data: data, cache: false, success: function(data) { for(var x in data) { $(".votes_up").find(id).html(data[x].vote_up); $(".votes_down").find(id).html(data[x].vote_down); } } }); });
So, when I create an element in the first place, I take the identifier of the record in the database and set it as the identifier of the elements. So what I'm trying to do is reference the exact element that was clicked and set it to HTML for data returned from PHP. I checked in Firebug and I get the correct data, but the number of votes does not change. Any ideas?
This is PHP for reference:
$query = "SELECT vote_up, vote_down FROM posts WHERE id = '".$id."'"; $result1 = mysql_query($query); $output = Array(); while ($row = mysql_fetch_array($result1)){ $output[] = Array( "vote_up" => $row['vote_up'], "vote_down" => $row['vote_down'], ); } echo json_encode($output);
json jquery ajax php
benhowdle89
source share