I have a delete function written in PHP and jQuery. that function does is deleting a post that the current user posted (tracks current user_id). But when i Click "Delete tweet" it doesn't delete it, and I don't understand why because the code doesn't work, it looks okay to me.
Thanks for your time and help.
Images:
Network tab:
Code snippets below:
$(function(){
$(document).on('click', '.deleteTweet', function(){
var tweet_id = $(this).data('tweet');
$.post('http://localhost:8888/twitter/core/ajax/deleteTweet.php', {showpopup:tweet_id}, function(data){
$('.popupTweet').html(data);
$('.close-retweet-popup,.cancel-it').click(function(){
$('.retweet-popup').hide();
});
});
});
$(document).on('click','.delete-it', function(){
var tweet_id = $(this).data('tweet');
$.post('http://localhost:8888/twitter/core/ajax/deleteTweet.php', {deleteTweet:tweet_id}, function(){
$('.retweet-popup').hide();
location.reload();
});
});
$(document).on('click', '.deleteComment', function(){
var tweet_id = $(this).data('tweet');
var commentID = $(this).data('comment');
$.post('http://localhost:8888/twitter/core/ajax/deleteComment.php', {deleteComment:commentID,tweet_id:tweet_id});
$.post('http://localhost:8888/twitter/core/ajax/popuptweets.php', {showpopup:tweet_id}, function(data){
$('.popupTweet').html(data);
$('.tweet-show-popup-box-cut').click(function(){
$('.tweet-show-popup-wrap').hide();
});
});
});
});
<?php
include '../init.php';
if(isset($_POST['deleteTweet']) && !empty($_POST['deleteTweet'])){
$tweet_id = $_POST['deleteTweet'];
$user_id = $_SESSION['user_id'];
//get tweet data from tweet id
$tweet = $getFromT->tweetPopup($tweet_id);
//create link for tweet image to delete from
$imageLink = '../../'.$tweet->tweetImage;
//delete the tweet from database
$getFromT->delete('tweets', array('tweetID' => $tweet_id, 'tweetBy' => $user_id));
//check if tweet has image
if(!empty($tweet->tweetImage)){
//delete the file
unlink($imageLink);
}
}
if(isset($_POST['showpopup']) && !empty($_POST['showpopup'])){
$tweet_id = $_POST['showpopup'];
$user_id = $_SESSION['user_id'];
$tweet = $getFromT->tweetPopup($tweet_id);
?>
<div class="retweet-popup">
<div class="wrap5">
<div class="retweet-popup-body-wrap">
<div class="retweet-popup-heading">
<h3>Are you sure you want to delete this Tweet?</h3>
<span><button class="close-retweet-popup"><i class="fa fa-times" aria-hidden="true"></i></button></span>
</div>
<div class="retweet-popup-inner-body">
<div class="retweet-popup-inner-body-inner">
<div class="retweet-popup-comment-wrap">
<div class="retweet-popup-comment-head">
<img src="<?php echo $tweet->profileImage;?>"/>
</div>
<div class="retweet-popup-comment-right-wrap">
<div class="retweet-popup-comment-headline">
<a><?php echo $tweet->screenName;?> </a><span>@<?php echo $tweet->username . ' ' . $tweet->postedOn;?></span>
</div>
<div class="retweet-popup-comment-body">
<?php echo $tweet->status . ' ' .$tweet->tweetImage;?>
</div>
</div>
</div>
</div>
</div>
<div class="retweet-popup-footer">
<div class="retweet-popup-footer-right">
<button class="cancel-it f-btn">Cancel</button><button class="delete-it" data-tweet="<?php echo $tweet->tweetID;?>" type="submit">Delete</button>
</div>
</div>
</div>
</div>
</div>
<?php }?>
</div>