1

I want to delete records from a MySQL database table using AJAX. I have done it with PHP and work fine. But I've not been able to get it through with AJAX.

AJAX

$(document).ready(function() {
    $(".confirm").click(function() {
        var bid = $(this).closest("div.box2").find('input[name="dbid"]').val();
        var dataString = 'id=' + bid;
        $.ajax({
            type: "POST",
            url: "<?php echo site_url('user/delete_article')?>",
            data: dataString,
            cache: false,
            success: function() {

                $.alert('Confirmed!');
            }
        });
    });
}); 

PHP

public function delete_article($id){
  $data['success']='';
  $data['error']='';
  include_once ('query/user_query.php');  
  $this->db->where('bid', $id);
  $data['countEarticle'] = $this->db->count_all_results('blog');
  if($data['countEarticle'] >= 1){

      $this->db->where('bid',$id);
      $this->db->delete('blog');

  }
  if($data['countEarticle'] <= 0){

  }          
}

HTML

<div class="box-footer box-comments box2" style="display: block;">
    <input type="hidden" name="dbid" value="<?php echo $draftfull['bid']?>">
    <p>
        <btn class="btn btn-azure btn-sm confirm"><i class="fa fa-trash-o"></i>Delete Article</btn>
    </p>
</div>

I need your help. What am I doing wrong?

3
  • What does user_query.php do? How is it relevant to this function? Commented Nov 24, 2018 at 4:41
  • 1
    Where'd the HTML tag <btn> come from? There isn't any such tag as far as I know. Commented Nov 24, 2018 at 4:49
  • <btn> is bootstrap. The user_query contains the user's data though is not used in this function. Commented Nov 24, 2018 at 8:40

2 Answers 2

2

try this code:-

public function delete_article(){
    $id=$this->input->post('bid');
    $data['success']='';
    $data['error']='';
    include_once ('query/user_query.php');

    $this->db->where('bid', $id);
    $data['countEarticle'] = $this->db->count_all_results('blog');
    if($data['countEarticle'] >= 1){

        $this->db->where('bid',$id);
        $this->db->delete('blog');

    }
    if($data['countEarticle'] <= 0){

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

2 Comments

Thanks, this worked and now understand the logic better now.
I change this line on the Jquery: var dataString = 'id='+bid; To: var dataString = 'bid='+bid;
1

try this:

<a href="javascript:;" class="btn btn-danger btn-xs mt-sweetalert swtalert" onclick="delete('<?php echo $draftfull["bid"] ?> ')" title="Delete"><span class="fa fa-ban"></span></a>

//ajax

function delete(id) {
   swal({
        title: "Are you sure to delete?",
        text: "Deleting will remove row from listing!",
        type: "error",
        showCancelButton: true,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "Yes!",
        cancelButtonText: "No",
        closeOnConfirm: true,
        closeOnCancel: true
    }, function (isConfirm) {
        if (isConfirm) {
            $.post(
                base_url + "user/delete_article", 
                {bid: id}, 
                function (data) {
                    if (data === "1") {
                        location.reload();
                    } else if (data === "0") {
                        swal("", "Error to deleting data.", "warning");
                    } else {
                        swal("", data[0], "error");
                    }
                });
            }
        });
}

//Controller

function delete_article($id){
    if ( $this->model_name->deleteDataById($this->input->post('bid') ) {
        die('1');
    }
    die('0');
}

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.