tanmoy sarkar tanmoy sarkar - 5 months ago 21
Javascript Question

jquery AJAX delete script from database

I am trying to delete the files from database by AJAX call. but i think i am misssing something to do so..I am getting only * del".$row1['id']." * instead of del id ... Please help me over it .

// My main file//

<html>
<body>
<script src="assets/js/jquery.min.js"></script>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h3><p align="center"><u>Filer ladda upp och ned</u></p></h3>
<div class="table-responsive">
<form enctype="multipart/form-data" action="" name="form" method="post">
<table id="files" border="1" align="center" id="table1" cellpadding="0" cellspacing="0">
<tr><td align="center">DOWNLOAD</td></tr>
<?php
$select=mysql_query("select * from upload order by id desc");
while($row1=mysql_fetch_array($select)){
$name=$row1['name'];
?>
<tr>
<td width="300">
<img src="files/tick.png" width="14" height="14"><p><?php echo $name ;?></p>
&nbsp; &nbsp;<a data-value=".$row1['id']." id=del".$row1['id'].">Delete</a>
</td>
<?php }?>
</tr>

</table>
<script src="assets/js/jquery.min.js"></script>
<script>

$(".del<?php echo $row1['id']; ?>").click(function() {
if (confirm("Are you sure want to delete?")) {
var delcart = $(this).data('value');
//alert(delcart);
$.ajax({
type: "GET",
url: "del_file.php",
data: {id : delcart},
success: function (data) {
if (data) {
//alert('data');
window.location.reload();
}
}
});
}
//alert($(this).data('value'));
});
</script>



</form>
</div>
</div>
<br />
<br />


</body>
</html>


//My del_file.php //

<?php
$del_id = $_GET['id'];
echo $del_id;
include'config.php' ;
$delete_item = mysql_query("delete from upload where id = $del_id ");
if($delete_item){
echo '1';
}else{
echo '0';
}
?>


Thank you .

Answer

You are doing the things in wrong way. $row1['id'] without <?php ?> it turn to be out only a string which is $row1['id'] and not an id from the loop. and another thing is your javascript selector is completely invalid , as it is outside of loop and it's not going to work at all. so better you follow the following code and make it possible in a better way.

Note again:

Your $row1['id'] is outside of while loop , so $row1['id'] is totally invalid and does not mean anything.

As you are targeting multiple files from the same page (let take an example that there are multiple elements and trigger for removing files connecting to same javascript function) You can use class selectors to get click event of multiple elements with same scope.

replace your delete anchor tag with :

<a data-value="<?= $row1['id'];?>" class="remove_file">Delete</a>

and then your javascript code should be like :

$(".remove_file").click(function() {
 var delcart = $(this).data('value');
      if (confirm("Are you sure want to delete?")) {
        $.ajax({
            type: "GET",
            url: "del_file.php",
            data: {id : delcart},
            success: function (data) {
                if (data) {
                    //alert('data');
                    window.location.reload();
                    }
                    }
                    });
      }
      //alert($(this).data('value'));
      });