Auto-update via AJAX

Hello! I have this question. There is a forum. There is a voting system through AJAX. But when you click on "+"or "-", the score does not change immediately, only through f5. Is it possible to do this without reloading the page? Here is the code:

$('button[id=up]').live.click(function(){
id=$(this).parent().attr('id');
pm=1; //,     1
// p=$(this).parent().parent().$('p').text();
// alert(p);
    $.ajax({
        url:"add.php",
        type:"POST",
        data: {id:id, pm:pm},
        //beforesend: alert("1"),
        success: function(d){alert(d)}
    }); 
});

And here is index.php:

while($row=mysql_fetch_array($res)){
        //row[0] eto id
        $t.="<span>$row[1] (id #<span id=$row[0]>$row[0]</span>)<div><form id=$row[0]><button id='up'>+</button><button id='down'>-</button></form><p>$r</p></div><br>$row[3]<hr /></span>";
        $w++;
    }
echo $t;

Answer 1, authority 100%

Yes

I don’t see any problems, send the request to the server, process it there, well, return the response to the page, for example

You sent the parameter $_POST[‘good’]=question/answer_ID, you should receive something like OK_question/answer_IDin response, and if you received it, change it an HTML element on the page that contains the old value.

It’s simple.

$('button[id=up]').live.click(function(){
id=$(this).parent().attr('id');
pm=1; //,     1
// p=$(this).parent().parent().$('p').text();
// alert(p);
    $.ajax({
        url:"add.php",
        type:"POST",
        data: {id:id, pm:pm},
        //beforesend: alert("1"),
        success: function(d){
     if d=   {
             //  
             var num = parseInt($("# ID   ").text());
             //      
              num=num+1; //   -1    
             $("# ID   ").text(num);
             }
        }
    }); 
});
while($row=mysql_fetch_array($res)){
        //row[0] eto id
        $t.="<span>$row[1] (id #<span id=$row[0]>$row[0]</span>)<div><form id=$row[0]><button id='up'>+</button><button id='down'>-</button></form><p>$r</p></div><br>$row[3]<hr /></span>";
        $w++;
    }
echo $t;

Brrr….

span id=$row[0]>$row[0] // what the hell?

<p>$r</p> // what is this?

I just don’t understand what kind of data you are pulling from the Table.
(id #<span id=$row[0]>$row[0]</span>)is this where you’re sort of inferring a score?


Answer 2

You can. All you need to do is to force the voice handler to return the current rating on an ajax request.