I want to make a favorite button with AJAX. I already wrote the PHP query, but I don't know how to make AJAX for it and connect it to my button.
I want something like favorite/unfavorite
$test = $_SESSION['ww'];
if (isset($_POST['submit']))
{
$x = $_SESSION['x'];
$SelectQry2 = "select * from favorites where User_Id = ".$test." and User_Post = ".$x."";
$slc = mysqli_query($link , $SelectQry2);
if (mysqli_num_rows($slc)> 0)
{
$DeleteQry = "DELETE from favorites where User_Post = ".$x."";
$del = mysqli_query($link , $DeleteQry);
}
else
{
$url = $_SERVER['REQUEST_URI'];
$InsertQry = "insert into favorites";
$InsertQry .="(`User_Id` ,`User_post`, `url`) VALUES";
$InsertQry .=" ('$test' ,'$x', '$url')";
$fav = mysqli_query($link, $InsertQry);
}
}
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
$SelectSql = " SELECT * , `upload_diy_ordinary`.`date` as datep FROM `users_final` LEFT OUTER JOIN `upload_diy_ordinary` ON `upload_diy_ordinary`.`User_ID`=`users_final`.`id` ";
$SelectSql .= " where `upload_diy_ordinary`.`ID` = $id";
$result = mysqli_query($link, $SelectSql);
while($row = mysqli_fetch_assoc($result))
{
$_SESSION['x'] = $row['id'];
<form method="post">
<input type="submit" name="submit" id="favorite" value="favorite" />
</form>
<?php }
} ?>
$(document).on('click', '#favorit', function(e) {
var data = $("#form").serialize();
$.ajax({
data: data,
type: "post",
url: "details.php",
success: function(data) {
alert("Data Save: " + data);
}
});
});
I'd appreciate it if you can help me