Page 1 of 1

Delete Data without Refreshing Page

Posted: 14 Jan 2020, 18:19
by JanMolendijk
Happy New Year Chief.... My first two weeks I spended on bed due to illness

I wanna use a script but it does not delete the item from my sql record

Code: Select all

  <?php
         $result = $database->prepare ("SELECT * FROM groups ");
         $result ->execute();
         for ($count=0; $row_member = $result ->fetch(); $count++){
         $id = $row_member['post_id'];
         ?>
      <tr class="delete_mem<?php echo $id ?>">
         <td><?php echo $row_member['user_name']; ?></td>
         <td><?php echo $row_member['group_title']; ?></td>
         <td><?php echo $row_member['group_date']; ?></td>
         <td width="80">
            <a class="btn btn-danger" id="<?php echo $id; ?>">Delete</a>
         </td>
      </tr>
      <?php } ?>
I think it does not detect the id in this javascript

Code: Select all

<script type="text/javascript">
    $(document).ready(function() {
        $('.btn-danger').click(function() {
            var id = $(this).attr("id");
            if (confirm("Are you sure you want to delete this Member?")) {
                $.ajax({
                    type: "POST",
                    url: "delete_member.php",
                    data: ({
                        id: id
                    }),
                    cache: false,
                    success: function(html) {
                        $(".delete_mem" + id).fadeOut('slow');
                    }
                });
            } else {
                return false;
            }
        });
    });
</script>
&nd here is the code delete_member.php

Code: Select all

<?php
include ('database.php');

$id = $_GET['id'];
$delete_data"delete from groups where post_id = '$id' ";
$database->exec($delete_data);
?>
I hope you can help me out with this one ????

Delete Data without Refreshing Page

Posted: 15 Jan 2020, 06:16
by Admin
Hello and Happy New Year.
1. Because the button is an anchor (<a>), add preventDefault() in the function from 'click', it prevents the action to not open/refresh the page. The function must contains the "event" parameter; in the code below is "ev":

Code: Select all

$('.btn-danger').click(function(ev){
  ev.preventDefault();
  var id = $(this).attr("id");
  // The rest of your code ...
)};
2. The ajax request sends data via POST, so, in php you should use $_POST.

Code: Select all

$id = $_POST['id'];

Delete Data without Refreshing Page

Posted: 15 Jan 2020, 10:20
by JanMolendijk
I changed the code but still the record is not getting deleted

Code: Select all

<script type="text/javascript">
    $(document).ready(function() {
   $('.btn-danger').click(function(ev){
  ev.preventDefault();
  var id = $(this).attr("id");
 
            if (confirm("Are you sure you want to delete this Member?")) {
                $.ajax({
                    type: "POST",
                    url: "delete_member.php",
                    data: ({
                        id: id
                    }),
                    cache: false,
                    success: function(html) {
                        $(".delete_mem" + id).fadeOut('slow');
                    }
                });
            } else {
                return false;
            }
        });
    });
</script>

Code: Select all

<?php
include ('database.php');

$id = $_POST['id'];
$delete_data"delete from groups where post_id = '$id' ";
$database->exec($delete_data);
?>
Could there something else what is missing ????

Delete Data without Refreshing Page

Posted: 15 Jan 2020, 10:28
by Admin
There is a syntax error in php.
Add "=" after $delete_data in php:

Code: Select all

$delete_data ="delete from groups where post_id = '$id' ";

Delete Data without Refreshing Page

Posted: 15 Jan 2020, 18:04
by JanMolendijk
O God that I did not see this `stupido me`....

Thank you very much for all the support `its working now`