Hello! I have this question. There is a forum. There is a voting system through AJAX . But when you click on the "+" or "-" score does not change immediately, only through f5 . Can I 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; 

    2 answers 2

    Can

    I do not see any problems, send a request to the server, process it there, well, return the answer to the page, let's say

    You sent the $ _POST ['good'] = ID_ parameter of the question or answer , you should receive something like OK_ID_ of the question or answer in response , and if you have, then change the 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; 

    Brrrr ....

    span id = $ row [0]> $ row [0] // what is it?

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

    I just do not understand what data you pull out of the table. (id # <span id = $ row [0]> $ row [0] </ span>) is this the type of evaluation you are making?

    • updated the answer - Artem
    • @Shrek, I updated the question. Can you select this item for an update? I still can not do it. - alex_90
    • @Shrek, I have a message id in $row[0] . <p>$r</p> - and this "bjaka" just shows the rating of the message. It needs to be changed. (Make a selection). I forgot! $r=row[4] is a rating in the database. - alex_90
    • Here! this is more correct. How do you see this rating on your page, is it the only element on the page or is there a lot of them? - Artem
    • one
      So you want messages to be ranked and not questions? then <p id = "mes_123"> 100 </ p> <p id = "mes_1123"> 0 </ p> <p id = "mes_12311"> 1 </ p> and select them var num = parseInt ( $ ("# mes_12311"). text ()); // then you need to write a new one there num = num + 1; // well, or -1 depending on whether you are plus or minus $ ("# mes_12311"). text (num); - Artem

    Can. It is only necessary to force the voice handler to issue a current rating on an ajax request.