0

I am deleting row in PHP using AJAX, but it shows Row can't delete. My Delete button is like this...

while ($row = mysqli_fetch_array($exec)) 
{
?>
    <tr>
        <td><?php echo $row["create_date"]; ?></td>
        <td><?php echo $row["bni_member_id"]; ?></td>
        <td><?php echo $row["bni_member_name"]; ?></td>
        <td><?php echo $row["bni_chapter_id"]; ?></td>
        <td><?php echo $row["bni_category_id"]; ?></td>
        <td><input type="button" name="delete" onclick="delFun()" value="delete" id="<?php echo $row["bni_member_id"]; ?>" class="btn btn-info btn-xs delete_data"/></td>
    </tr>
    <?php
}
?>

I call a function on the click button. Function like this. It always Executes part can't delete. I think there is a problem with

data: {del_id: del_id},

I think there is a problem with Data... but I can't resolve it.

   var delfin;
$(document).ready(function () {
    delFun = function () {
        $('.delete_data').click(function () {
            var del_id = $(this).attr("name");
            var $ele = $(this).parent().parent();
            $.ajax({
                url: "phpfile/delete.php",
                method: "POST",
                data: {del_id: del_id},
                success: function (data) {
                    if(data=="YES"){
                        $ele.fadeOut().remove();
                     }else{
                            alert("can't delete the row")
                     }
                }
            });
        });
    } 
}); 

And My PHP file is in another directory like this

<?php
    include('../../connection.php');
    $music_number = $_POST['del_id'];
    //echo $music_number;
    $qry = "DELETE FROM bni_member WHERE bni_member_id ='$music_number'";
    $result=mysql_query($qry);
    if(isset($result)) {
       echo "YES";
    } else {
       echo "NO";
    }
?>

My table is like

CREATE TABLE `bni_member` (
 `bni_member_id` int(11) NOT NULL AUTO_INCREMENT,
 `bni_member_name` text,
 `bni_member_mobile` varchar(13) DEFAULT NULL,
 `bni_member_email` text,
 `bni_member_website` text,
 `bni_member_bio` text,
 `bni_member_export_to` text,
 `bni_member_import_from` text,
 `bni_member_want_to_connect_to` text,
 `bni_member_company` text,
 `bni_chapter_id` int(11) DEFAULT NULL,
 `bni_category_id` int(11) DEFAULT NULL,
 `bni_member_address` text,
 `bni_member_commitee` text,
 `bni_member_profilepic` longblob NOT NULL,
 `bni_member_logo` longblob NOT NULL,
 `create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
 `is_active` int(1) NOT NULL DEFAULT '1',
 `is_delete` int(1) NOT NULL DEFAULT '0',
 `last_update` text,
 `del_status` varchar(50) NOT NULL,
 PRIMARY KEY (`bni_member_id`),
 KEY `bni_chapter_id` (`bni_chapter_id`),
 KEY `bni_category_id` (`bni_category_id`),
 KEY `bni_member_id` (`bni_member_id`),
 CONSTRAINT `bni_member_ibfk_1` FOREIGN KEY (`bni_chapter_id`) REFERENCES 
`bni_chapter` (`bni_chapter_id`),
 CONSTRAINT `bni_member_ibfk_2` FOREIGN KEY (`bni_category_id`) 
REFERENCES `bni_category` (`bni_category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=225 DEFAULT CHARSET=latin1
11
  • don't use mysql_query it is deprecated now. Commented Jul 31, 2019 at 6:19
  • any error are you getting? Commented Jul 31, 2019 at 6:20
  • your API usage is confusing, on the tables, you use mysqli_fetch_array, then on the PHP ajax processing, you use mysql_query, just use mysqli Commented Jul 31, 2019 at 6:26
  • I updated this but still I gives row can't delete $result=mysqli_query($qry); if(isset($result)) { echo "YES"; } else { echo "NO"; } @Ghost Commented Jul 31, 2019 at 6:34
  • 1
    check var del_id = $(this).attr("name") should be var del_id = $(this).attr("id") Commented Jul 31, 2019 at 6:44

3 Answers 3

1

The first problem is that, you're getting name attribute from your delete button. You can pass bni_member_id as a data attribute instead of using element id attribute. It can cause confusion.

And the second problem is that, you're using both onlick attribute and jQuery's click method. Using one them is pretty enough. Your input button will look like this:

<td><input type="button" name="delete" value="delete" class="btn btn-info btn-xs delete_data" data-bni-member-id="<?= $row["bni_member_id"]; ?>" /></td>

Then your js will look like this:

$(document).ready(function () {
    $('.delete_data').click(function () {
        var del_id = $(this).data("bni_member_id");
        var $ele = $(this).parent().parent();
        $.ajax({
            url: "phpfile/delete.php",
            method: "POST",
            data: {del_id: del_id},
            success: function (data) {
                if(data=="YES"){
                    $ele.fadeOut().remove();
                } else {
                    alert("can't delete the row")
                }
            }
        });
    });
});

I hope this helps you.

Sign up to request clarification or add additional context in comments.

Comments

0

you can change delete button

 <input type="button" name="delete" onclick="delFun()" value="delete" data-id="<?php echo $row["bni_member_id"]; ?>" class="btn btn-info btn-xs delete_data"/>

And Jquery Event

    $(document).on("click",".delete_data",function () {

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

Comments

0

I am sharing my code with you try like this

       <tr>
          <td><?php echo $i; ?></td>
          <td><?php echo $row["exam_name"]; ?></td>
          <td><?php echo $row["exam_date"]; ?></td>
          <td><?php echo $row["exam_status"]; ?></td>
          <td><?php echo $row["exam_comment"]; ?></td>
         <td><button type="button" name="delete_btn" id="<?php echo $row["examid"]; ?>" class="btn btn-sm btn-danger btn_delete"><i class="fa fa-trash"></i></button></td> 
      </tr>

Add script

<script type="text/javascript">
    $(document).on('click', '.btn_delete', function(){  
           // var id=$(this).data("id3");
            var el = this;
        var id = this.id;
        var splitid = id.split("_");

        // Delete id
        var deleteid = splitid[1];  
           if(confirm("Are you sure you want to delete this?"))  
           {  
                $.ajax({  
                     url:"path.....",  
                     method:"POST",  
                     data:{id:id},  
                     dataType:"text",  
                     success:function(data){  
                           $(el).closest('tr').css('background','#d31027');
                $(el).closest('tr').fadeOut(800, function(){      
                    $(this).remove();
                });  

                     }  
                });  
           }  
      }); 
 </script>

Comments

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.