0

I am trying to delete the files from the database by AJAX call. but I think I am missing 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 .

10
  • have you debug your code, that $del_id to delete query is getting or not ? Commented Jul 4, 2016 at 9:45
  • Do you get proper id in alert(delcart); Commented Jul 4, 2016 at 9:45
  • @Anandjain no its not working. Commented Jul 4, 2016 at 9:46
  • @jitendrapurohit no in console only del".$row1['id']." this shows. Commented Jul 4, 2016 at 9:47
  • 1
    Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the PDO database extensions. Start here its really pretty easy Commented Jul 4, 2016 at 9:51

2 Answers 2

2

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'));
      });
Sign up to request clarification or add additional context in comments.

2 Comments

thank you i got my error as well as my answer . this my day .
@tanmoysarkar glad it did help you out :)
-1

Try this

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

2 Comments

Why should the OP try this?
This is not going to work as the javascript code is outside of while loop and $row1['id'] does not even exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.