Delete Record from MySQL using AJAX and PHP in Codeigniter












1















I want to delete records from MySQL database table using AJAX. I have done it with PHP and work fine. But I've not be 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?










share|improve this question























  • What does user_query.php do? How is it relevant to this function?

    – DFriend
    Nov 24 '18 at 4:41






  • 1





    Where'd the HTML tag <btn> come from? There isn't any such tag as far as I know.

    – DFriend
    Nov 24 '18 at 4:49











  • <btn> is bootstrap. The user_query contains the user's data though is not used in this function.

    – SholleyonlineJava
    Nov 24 '18 at 8:40
















1















I want to delete records from MySQL database table using AJAX. I have done it with PHP and work fine. But I've not be 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?










share|improve this question























  • What does user_query.php do? How is it relevant to this function?

    – DFriend
    Nov 24 '18 at 4:41






  • 1





    Where'd the HTML tag <btn> come from? There isn't any such tag as far as I know.

    – DFriend
    Nov 24 '18 at 4:49











  • <btn> is bootstrap. The user_query contains the user's data though is not used in this function.

    – SholleyonlineJava
    Nov 24 '18 at 8:40














1












1








1








I want to delete records from MySQL database table using AJAX. I have done it with PHP and work fine. But I've not be 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?










share|improve this question














I want to delete records from MySQL database table using AJAX. I have done it with PHP and work fine. But I've not be 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?







php ajax codeigniter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 4:08









SholleyonlineJavaSholleyonlineJava

479




479













  • What does user_query.php do? How is it relevant to this function?

    – DFriend
    Nov 24 '18 at 4:41






  • 1





    Where'd the HTML tag <btn> come from? There isn't any such tag as far as I know.

    – DFriend
    Nov 24 '18 at 4:49











  • <btn> is bootstrap. The user_query contains the user's data though is not used in this function.

    – SholleyonlineJava
    Nov 24 '18 at 8:40



















  • What does user_query.php do? How is it relevant to this function?

    – DFriend
    Nov 24 '18 at 4:41






  • 1





    Where'd the HTML tag <btn> come from? There isn't any such tag as far as I know.

    – DFriend
    Nov 24 '18 at 4:49











  • <btn> is bootstrap. The user_query contains the user's data though is not used in this function.

    – SholleyonlineJava
    Nov 24 '18 at 8:40

















What does user_query.php do? How is it relevant to this function?

– DFriend
Nov 24 '18 at 4:41





What does user_query.php do? How is it relevant to this function?

– DFriend
Nov 24 '18 at 4:41




1




1





Where'd the HTML tag <btn> come from? There isn't any such tag as far as I know.

– DFriend
Nov 24 '18 at 4:49





Where'd the HTML tag <btn> come from? There isn't any such tag as far as I know.

– DFriend
Nov 24 '18 at 4:49













<btn> is bootstrap. The user_query contains the user's data though is not used in this function.

– SholleyonlineJava
Nov 24 '18 at 8:40





<btn> is bootstrap. The user_query contains the user's data though is not used in this function.

– SholleyonlineJava
Nov 24 '18 at 8:40












2 Answers
2






active

oldest

votes


















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){

}
}





share|improve this answer
























  • Thanks, this worked and now understand the logic better now.

    – SholleyonlineJava
    Nov 24 '18 at 8:33











  • I change this line on the Jquery: var dataString = 'id='+bid; To: var dataString = 'bid='+bid;

    – SholleyonlineJava
    Nov 24 '18 at 8:38



















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');
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455065%2fdelete-record-from-mysql-using-ajax-and-php-in-codeigniter%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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){

    }
    }





    share|improve this answer
























    • Thanks, this worked and now understand the logic better now.

      – SholleyonlineJava
      Nov 24 '18 at 8:33











    • I change this line on the Jquery: var dataString = 'id='+bid; To: var dataString = 'bid='+bid;

      – SholleyonlineJava
      Nov 24 '18 at 8:38
















    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){

    }
    }





    share|improve this answer
























    • Thanks, this worked and now understand the logic better now.

      – SholleyonlineJava
      Nov 24 '18 at 8:33











    • I change this line on the Jquery: var dataString = 'id='+bid; To: var dataString = 'bid='+bid;

      – SholleyonlineJava
      Nov 24 '18 at 8:38














    2












    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){

    }
    }





    share|improve this answer













    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){

    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 5:05









    Parvej AlamParvej Alam

    2167




    2167













    • Thanks, this worked and now understand the logic better now.

      – SholleyonlineJava
      Nov 24 '18 at 8:33











    • I change this line on the Jquery: var dataString = 'id='+bid; To: var dataString = 'bid='+bid;

      – SholleyonlineJava
      Nov 24 '18 at 8:38



















    • Thanks, this worked and now understand the logic better now.

      – SholleyonlineJava
      Nov 24 '18 at 8:33











    • I change this line on the Jquery: var dataString = 'id='+bid; To: var dataString = 'bid='+bid;

      – SholleyonlineJava
      Nov 24 '18 at 8:38

















    Thanks, this worked and now understand the logic better now.

    – SholleyonlineJava
    Nov 24 '18 at 8:33





    Thanks, this worked and now understand the logic better now.

    – SholleyonlineJava
    Nov 24 '18 at 8:33













    I change this line on the Jquery: var dataString = 'id='+bid; To: var dataString = 'bid='+bid;

    – SholleyonlineJava
    Nov 24 '18 at 8:38





    I change this line on the Jquery: var dataString = 'id='+bid; To: var dataString = 'bid='+bid;

    – SholleyonlineJava
    Nov 24 '18 at 8:38













    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');
    }





    share|improve this answer






























      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');
      }





      share|improve this answer




























        1












        1








        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');
        }





        share|improve this answer















        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');
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 7:16









        KarthzDIGI

        311212




        311212










        answered Nov 24 '18 at 5:01









        PHP GeekPHP Geek

        1,3511718




        1,3511718






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455065%2fdelete-record-from-mysql-using-ajax-and-php-in-codeigniter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ottavio Pratesi

            Tricia Helfer

            15 giugno