php - Ajax favorite/unfavorite button -
i'm trying implement ajax on favourite/unfavourite button. idea behind when click star changes yellow , when click again, changes grey , on. adds , deletes data db.
before had this
<?php include("classes/event.class.php"); $m = new event(); $arrayallevents = $m->getnonfavo(); $arrayfavorites = $m->getfavo(); $db = new db(); while ($row = mysqli_fetch_assoc($arrayfavorites)) { $unfavoriteid = $row['f_id']; $uid = $_session['u_id']; } if(isset($_post['favorite_row'])) { $uid = $_session['u_id']; $fid = $_post['id_to_be_favo']; if(!mysqli_query($db->conn, "insert favorites (u_id, n_id, f_boolean) values ('". $db->conn->real_escape_string($uid) ."' , '". $db->conn->real_escape_string($fid) ."' , '". $db->conn->real_escape_string("1") ."')")) { echo mysqli_error($db->conn); } } if(isset($_post['unfavorite_row'])) { $unfid = $_post['id_to_be_unfavo']; if(!mysqli_query($db->conn, "delete favorites f_id ='".$unfid."'")) { echo mysqli_error($db); } } ?>
and field php echo form
<?php echo "<form method='post'> <input type ='hidden' name='id_to_be_favo' value='".$a['n_id']."' /> <input type='submit' class='favoritefalse' id='favobtn' name='favorite_row' value='favorite' /> </form> <div class='clearfix'> </div>"; ?>
i'm trying change form button , use jquery/ajax switch it. i've been trying different solutions i've found online, none of them seem work.
what best way implement this?
kind regards
<input type='button' class='favorite' id='<?php echo $a['n_id']; ?>' value='favorite' /> <div class='clearfix'> </div>";
using
$(function() { $(".favorite").on("click".function() { $.post("whatever.php", {"favorite_row":this.id}, function(data) { $("#star").html('<img/>',{"src":data.fave==yes"?"true.png":"false.png"}); }); });
now return {"fave":"yes"} if favorited
Comments
Post a Comment