U
U
user12132018-03-28 04:18:37
AJAX
user1213, 2018-03-28 04:18:37

Why is the post not being deleted with AJAX?

I'm trying to delete an entry from the database and hide the element using AJAX, but for some reason the deletion doesn't happen.
Classes with names of pictures are displayed on the page , when clicking on the delete button the corresponding class should be hidden .
Page file.

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" type="text/css" href="style2.css">
<script>
$('.but').on('click', function() {
  // действия, которые будут выполнены при наступлении события...
  var count=$(this).attr("id");
});
function del(){
    var name = count;
    
    $.ajax({
        type: "POST",
        url: "delete.php",
        data: {fname:name}
    success: function() {
                $("."+name).remove();
            },
  })
}
    </script>
</head>
<body>
<?php
$delet=$_GET[name];
$category=$_GET[category];
$host = 'localhost'; // адрес сервера 
$database = 'imagess'; // имя базы данных
$user = 'admin'; // имя пользователя
$password = '12345'; // пароль
$link = mysqli_connect($host, $user, $password, $database) 
    or die("Ошибка " .  mysqli_error($link));
  
if ($delet!='')
{
mysqli_query($link,"delete FROM imagess WHERE name='".$delet."'");
}

$res = mysqli_query($link,"SELECT name,showname FROM imagess WHERE category='".$category."'");

  
  
  ?>  
 	<div class="wrap">
  <?php
 while($row = mysqli_fetch_array($res)) {
$src='upload/'.$row['name'];
?>
<div class="<?php echo $row['showname']; ?>">
<div class="block">
<div   style="height:400px;width:450px;overflow:hidden; text-align: center;float:left;background-color:#8c95a3;">
 <img style="max-height:400px;" src="<?php echo $src; ?>"/>
 
</div>
<input type="button" class="but" id="<?php echo $row['name'];?> " value="Удалить" onClick = "del()" />

</div>
</div>

<?php		
}
?>

</div>
<?php
mysqli_close($link);
?>
</body>
</html>

delete file
<?php
$name=$_POST['fname'];

$host = 'localhost'; // адрес сервера 
$database = 'imagess'; // имя базы данных
$user = 'admin'; // имя пользователя
$password = '12345'; // пароль
$link = mysqli_connect($host, $user, $password, $database) 
    or die("Ошибка " .  mysqli_error($link));
  if ($name!='')
{
mysqli_query($link,"delete FROM imagess WHERE name='".$name."'");
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sanovskiy, 2018-03-28
@Sanovskiy

I'd like to start off by saying that my eyes bled multiple times while reading this code.
I'm already silent about the salad of PHP and HTML.
In order.
1. Read about scopes in JS This variable will not be available further in the code, so it will not fall into the general scope. Declare it outside of the callback. https://habrahabr.ru/post/78991/ 2. Why do you need duplicate code? Deleting both in the main file and in the ajax handler. https://kb.mista.ru/article.php?id=273 3. The code is vulnerable to injections
Let's say I call delete.php?name=' OR 1 OR name!='
What do you think will happen?
https://habrahabr.ru/post/148151/
These are far from all the problems in your code, but start by fixing at least these.
And give up MySQLi already. This extension has long been obsolete. Use PDO

T
Talyan, 2018-03-28
@flapflapjack

So when you click, only var count=$(this).attr("id");
and the task function hangs in the air and is never executed at all.
Move the bracket.

$('.but').on('click', function() {
  // действия, которые будут выполнены при наступлении события...
  var count=$(this).attr("id");


    var name = count;
    
    $.ajax({
        type: "POST",
        url: "delete.php",
        data: {fname:name}
    success: function() {
                $("."+name).remove();
            },
  })

});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question