0

i'm tyring to delete some records from my database but without going to the delete.php file or refreshing the page.

i tried this code:

lieferant.php

   <td> <?php echo    $row['lieferantID']; ?>  </td>

   <td> <?php echo    $row['lieferantname']; ?>  </td>

   <td> <?php echo    $row['kontakt']; ?>  </td>

   <td> <?php echo    $row['mail']; ?>  </td>

<td>  <button type="button" id="<?php echo $row['lieferantID']; ?>" class="delete-btn">Delete</button></td>


<script type="text/javascript" >
        $(function() {

            $(".delete-btn").click(function() {
                var del_id = $(this).attr("id");
                var info = {"lieferantID": del_id};
                if (confirm("Are you sure?")) {
                    $.ajax({
                        type : "POST",
                        url : "delete.php", //URL to the delete php script
                        data : info,
                        success : function() {
                        }
                    });
                    $(this).parents(".record").animate("fast").animate({
                        opacity : "hide"
                    }, "slow");
                }
                return false;
            });
        });
 </script>

delete.php

$lieferantID=$_POST['lieferantID'];
$delete=mysqli_query($connection,"DELETE FROM 'lieferant' WHERE lieferantID='$lieferantID'");
$result = mysqli_query($delete) or die(mysqli_error());

Unfortunetely it doesn't work. When i put the ID of the row manuelly in the query it will be deleted without going to the delete.php, but still i have to refresh the page to see that the record is gone, so my question is:

1.how can i pass the 'lieferantID' (the ID of the row) to my delete.php, so that it works automatically 2.how can the page "refresh" itself?

thanks for any help :)

Braman
  • 11
  • 5
  • `var del_id = $(this).attr("lieferantID");` Does your button actually have an attribute called `lieferantID`? – brombeer Feb 26 '21 at 13:53
  • Hi this `$(this).attr("lieferantID")` should be `$(this).attr("id")` . Also add this `type="button"` to your button – Swati Feb 26 '21 at 13:58
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Feb 26 '21 at 13:59
  • Typo: `'lieferantID' + del_id;` should be `'lieferantID=' + del_id;` or better maintain it as an object not a string – Dharman Feb 26 '21 at 14:00
  • @Swati I changed everything what y'all said but it still don't work :/ – Braman Feb 26 '21 at 14:10
  • are you seeing any error inside browser console ? – Swati Feb 26 '21 at 14:27

2 Answers2

1

you should change

var del_id = $(this).attr("lieferantID");

to

var del_id = $(this).attr("id");

since you use id in button element.

and try this

 var info = {lieferantID: del_id};

instead of

var info = 'lieferantID' + del_id;

for the refresh part you can use this

setInterval('location.reload()', 1000);

inside success
so the final code look like this:

<script type="text/javascript" >
        $(function() {

            $(".delete-btn").click(function() {
                var del_id = $(this).attr("id");
                var info = {lieferantID: del_id};
                if (confirm("Are you sure?")) {
                    $.ajax({
                        type : "POST",
                        url : "delete.php", //URL to the delete php script
                        data : info,
                        success : function() {
                            setInterval('location.reload()', 1000);
                        }
                    });
                    $(this).parents(".record").animate("fast").animate({
                        opacity : "hide"
                    }, "slow");
                }
                return false;
            });
        });
 </script>

or you can hide that row after success response without refreshing the page.

0

You're sending wrong thing to post request. Use this

var info = {"lieferantID": del_id};
    $.ajax({
        type : "POST",
        url : "delete.php",
        data : info,
        dataType: 'json',
        success : function() {
            setInterval('location.reload()', 1000);
        }
    });