I decided on the main page to make the button "Add to favorites" for each news without reloading the page.

Favorites - the type of module on the site.

Visual example:

- Добавить в избранное (id1) - Добавить в избранное (id2) - Добавить в избранное (id3) - Добавить в избранное (id4) 

For each news you need to assign an Ajax function:

 <script type="text/javascript"> $(function() { $("#send").click(function(){ var newsid = $("#newsid").val(); $.ajax({ type: "POST", url: "favor.php", data: {"newsid": newsid}, cache: false, success: function(response){...... и тд. 

It turns out bad code. Is it possible to send different data with one writing of one function above? After all, all the buttons "add to favorites" have different id. How to apply it? Something like

 ...onclick="vote(5);"... ...function vote(i)... 

But how to take values ​​from name to send a post? Explain in more detail.

    2 answers 2

    Basics of jQuery, as it were.

     <button class="news" id="send1" value="1" type="button">В избранное новость 1</button> <button class="news" id="send2" value="2" type="button">В избранное новость 2</button> <button class="news" id="send3" value="3" type="button">В избранное новость 3</button> <button class="news" id="send4" value="4" type="button">В избранное новость 4</button> $('.news').click(function(){ $.ajax({ type: "POST", url: "favor.php", data: {"newsid": $(this).val()},...... }); 
    • Cool, check. ____ How to get an id? It is necessary to change the class after pressing, such as an asterisk became full. - Rammsteinik
    • one
      $ (this) .attr ('id') the same! - knes
    • It turned out this way, but does not rob $ (". Fav"). Click (function () {var uid = <? Php echo $ u_id;?>; $ .Ajax ({type: "POST", url: "favorite_add.php ", data: {" user ": uid," itemfid ": $ (this) .val ()}, cache: false, success: function (response) {if (response == 0) {}}}); return false;}); <button style = "padding-left: 3px; padding-right: 3px;" rel = "tooltip_bottom" title = "Add to favorites" class = "fav" type = "button" value = "<? php echo $ row_2 ['id'];?>"> </ button> - Rammsteinik
    • I will deal with id later. Here pkhp: <? include ('bd.php'); if (! empty ($ _ POST ['user'])) {$ uid = mysql_real_escape_string (trim ($ _ POST ['user']))); $ id = intval (trim ($ _ POST ['itemfid'])); $ result = mysqli_query ($ db, "paste it into the database here"); if ($ result == true) {echo 0; } else {echo 1; }}?> - Rammsteinik

    It is not necessary to make different values, you can just give all the buttons the same class and then use js to catch a click

     $("body").on("click",".badges",function() 

    To transfer attributes you can simply add <button id_favorite="тут само ID"></button and transfer it to ajax and do what you want

     data:{"id_favorite":$(this).attr("id_favorite"),} 

    I did this:

    HTML + PHP - I hung up on the id_badges button and pasted the ID of what you need to cram into your favorites

     <a id_badges="<? echo $product_arr['id']; ?>" type_badges="add_favorites" class="favorites icons badges" <? if($product_arr['star'] == '1'){echo'id="star"';}; ?> ><span class="glyphicon glyphicon-star"></span></a> 

    AJAX itself, everything is simple, I pass the attributes id_badges and type_badges through POST

     $("body").on("click",".badges",function(){ $.ajax({ type:'POST', url:"/php/deleted.php", data:{"id_badges":$(this).attr("id_badges"),"type_badges":$(this).attr("type_badges")}, success:function(data){ $(".refresh").html(data); } }); }); 

    PHP handler - I process it and add it to the database, in the end I need to update the block update as well

     if($type_badges == "add_favorites"){ $data = mysql_fetch_assoc(mysql_query("SELECT `user`,`star` FROM `product` WHERE `user`='".$user."' && `id`='".$id_badges."' LIMIT 1")); if($data['star'] == '0'){ $star = "1"; print '<div class="alert succes alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><span class="glyphicon glyphicon-star"></span> Добавлено в избранное</div>'; } else if($data['star'] == '1'){ $star = "0"; print '<div class="alert succes alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><span class="glyphicon glyphicon-trash"></span> Удалено из избранного</div>'; } mysql_query("UPDATE `product` SET `star`='".$star."' WHERE `id`='".$id_badges."'"); };