On the site users have option to add other users as a friend. Now I want to make option to delete some user from the friend list. The problem is that I don't know how exactly to do it with jquery part.
This is the button for remove
<a href="#" class="delete" id="'.$row['id'].'"><i class="fa fa-times pull-right"></i></a>
Then this is the jquery part
$(document).ready(function() {
$('.delete').click(function() {
var parent = $(this).closest('media-heading');
$.ajax({
type: 'get',
url: 'misc/friendRemove.php',
data: 'ajax=1&delete=' + $(this).attr('id'),
beforeSend: function() {
parent.animate({'backgroundColor':'#fb6c6c'},300);
},
success: function() {
parent.fadeOut(300,function() {
parent.remove();
});
}
});
});
$('.delete').confirm({
text: "Are you sure you want to delete?",
title: "Confirmation required",
confirmButton: "Yes",
cancelButton: "No",
post: true,
confirmButtonClass: "btn-danger",
cancelButtonClass: "btn-default",
dialogClass: "modal-dialog modal-lg"
});
});
and the friendRemove.php
if(isset($_POST['id']) {
$friend_id = $_POST['id'];
$value = $pdo->prepare('UPDATE user_friends SET isDeleted = `1` and isActive = `0` WHERE friend_id= ?');
$value->bindParam(1, $friend_id, PDO::PARAM_INT);
$value->execute();
$result = $value->fetch();
}
Where I'm wrong?
On Console in the Chrome I get this error so far
friendRemove.php?ajax=1&delete=37 500 (Internal Server Error)
UPDATE: I'm very new into jquery and I used this from one tutorial..
UPDATE: this is the table if it's matter but I can't update it even from sql query in the phpmyadmin
CREATE TABLE IF NOT EXISTS `user_friends` (
`friend_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`isActive` tinyint(1) NOT NULL DEFAULT '0',
`isDeleted` tinyint(1) NOT NULL DEFAULT '0',
`friendsSince` datetime NOT NULL,
PRIMARY KEY (`friend_id`,`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
UPDATE: Query should be like this
UPDATE user_friends SET isDeleted = 1, isActive = 0 WHERE friend_id = ?
instead of this
UPDATE user_friends SET isDeleted = 1 AND isActive = 0 WHERE friend_id = ?
Your ajax type is
type: 'get',
So instead of POST
you have to use GET and closing issue of isset
function
if(isset($_GET['id'])) { // here closing issue also
$friend_id = $_GET['id'];
Remove backtick
from value of isDeleted and isActive
$value = $pdo->prepare('UPDATE user_friends SET isDeleted = 1 and isActive = 0 WHERE friend_id= ?');
SO you full php code would be
if (isset($_GET['delete'])) {
$friend_id = $_GET['delete'];
$value = $pdo->prepare('UPDATE user_friends SET isDeleted = 1 and isActive = 0 WHERE friend_id= ?');
$value->bindParam(1, $friend_id, PDO::PARAM_INT);
$value->execute();
}
In addition to making a GET request, if you are looking for the id
variable in the URL request, you have to explicitly set it ... right now you're looking for $_POST['id']
, but it looks like you should be looking for $_GET['delete']
from how you've set the URL parameters via the AJAX request.
Remove the ´ from the code.
$value = $pdo->prepare('UPDATE user_friends SET isDeleted = `1` and isActive = `0` WHERE friend_id= ?'
Modified line:
$value = $pdo->prepare('UPDATE user_friends SET isDeleted = 1 and isActive = 0 WHERE friend_id= ?'