Hartag96 Hartag96 - 3 months ago 9
Javascript Question

Incorrect value of $.post. Selection in JQuery

I have file delete.php witch contains

<?php
$folder = "./fak/";
$filename = $_POST['name'];
unlink($folder.$filename);
?>


and index.html file

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".delete").click(function(){

$.post("delete.php",
{
name:$(".delete").attr("value")
},
function(data, status){
alert("data:"+ data + "\n Status:" + status)
location.reload();
});
});
});
</script>
</head>
<body>
<div class="item">
<img src="./fak/1.png">
<button class="delete" value="1.png"> delete img </buttom>
</div>

<div class="item">
<img src="./fak/2.png">
<button class="delete" value="2.png"> delete img </buttom>
</div>

</body>
</html>


And here is my problem, no matter which button I insert delete.php remove 1.png. When I insert button once more jQuery alert that the file 1.png don't exist.

So my problem is in selection correct value and after execute php file clear value 'name'

Answer

$(".delete").attr() will retrun first always, replace with

   $(".delete").click(function(){
      $.post("delete.php",
      {
        name:$(this).attr("value")
      },
      function(data, status){
        alert("data:"+ data + "\n Status:" + status)
        location.reload();
      });