update span content with ajax
How could I make the votes update directly? Just like the votes on this sit开发者_如何学Goe
I have this function:
function ratePost(id, rating) {
$.post("ratepost.php", {postID: id, rating: rating}, function(data){alert(data+" return val"); });
}
<span class="karma"><?php echo $rating ?></span>
i think you need to do 2 things in order for it to work:
- set a id to your display span and leave it empty (no need for the php echo), eg
<span id = 'rating'...></span>
- in your ajax callback function, do something like
function(data){document.getElementById("rating").innerHTML = data;}
Seems you are with a javascript library like jQuery.
So you can target your span like that in your callback :
function(data){ $(".karma").html(data) }
Here it is what you want (I think :-)) :
function ratePost(id, rating) {
$.post("ratepost.php", {postID: id, rating: rating}, function(data){
var current = $(".karma").html();
$(".karma").html( current + 1 );
});
}
<span class="karma"><?php echo $rating ?></span>
精彩评论