filemonczyk filemonczyk - 25 days ago 9
Javascript Question

refreshing certain data after action

This is my code:

<input type="tekst" id="<?php echo $item->getId();?>">
<script>
document.getElementById("<?php echo $item->getId();?>").value =<?php echo getVotesValue($item->getId());?>
</script>


I would like to have my actual like amount displayed, right after user clicks like button, now to see the new likes amount I have to refresh the page, likes are held on server.

this is my code responsible for adding like:

<script>
$(function () {
$('a.like').on('click', function () {
var url = $(this).attr('href');
$.post(url, {}, function () {
});
return false;
});
});


so once again my goal is to have new value of likes displayed in
<input>
tag after clicking I tried to do :

$(function () {
$('a.like').on('click', function () {
var url = $(this).attr('href');
$.post(url, {}, function () {
document.getElementById("<?php echo $item->getId();?>").value =<?php echo getVotesValue($item->getId());?>
});
return false;
});
});


but it is not working as expected

Answer

you shoud use data-...

<a href="#" class="vote-up" data-id="<?php echo $item->getId(); ?>">Vote Up</a>

and click function

$('a.vote-up').click(function(e){
    var id = $(this).data('id');
    var url = 'likes.php?do=vote_up&id=' + id; // change to your PHP file
    $.post(url, {}, function(result){
        // you should return the total of likes in result
        $('#'+ id).val(result);
    });
});