Welcome Guest, Not a member yet? Register   Sign In
codeigniter and ajax based update delete problem
#1

[eluser]Ahmed Iqbal[/eluser]
Hello Friends,
i need your help in Codeigner and ajax based update/delete.

first of all in view area i've following code.
Code:
[Jquery And Link Disappear Color Effect Library]
$(function() {
$(".delete_button").click(function() {
var id = $(this).attr("id");
var dataString = 'id='+ id;
var parent = $(this).parent();
    
$.ajax({
   type: "POST",
   url: "<?=base_url();?>index.php/admin/deleteSMS",
   data: dataString,
   cache: false,

   beforeSend: function()
   {
   parent.animate({'backgroundColor':'#fb6c6c'},300).animate({ opacity: 0.35 }, "slow");;
   },
   success: function()
   {
  
    parent.slideUp('slow', function() {$(this).remove();});
    
  }
  
});

return false;
    });
});

[removed]
<style type="text/css">
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
.comment_box
{
background-color:#D3E7F5; border-bottom:#ffffff solid 1px; padding-top:3px
}
a
    {
    text-decoration:none;
    color:#d02b55;
    }
    a:hover
    {
    text-decoration:underline;
    color:#d02b55;
    }
    *{margin:0;padding:0;}
    ol.update
    {list-style:none;font-size:1.2em; }
    ol.update li{ height:50px; border-bottom:#dedede dashed 1px; background-color:#ffffff}
    
    #main
    {
    width:500px; margin-top:20px; margin-left:100px;
    font-family:"Trebuchet MS";
    }
    .delete_button
    {
    margin-left:10px;
    font-weight:bold;
    float:right;
    }
    
    
    h1
    {
    
    margin-top:20px;
    margin-bottom:20px;
    background-color:#dedede;
    color:#000000;
    font-size:18px;
    }
</style>
<a href="#">sms_id;?&gt;" class="delete_button"><img src="&lt;?=base_url();?&gt;img/icons/page_white_delete.png"></a>

Controller Code
Code:
public function deleteSMS() {
        $this->db->where('sms_id', $_POST['id']);
        $this->db->limit(1,0);
        $this->db->delete('sms');
        if ($this->db->affected_rows() > 0) {
        // $data['result']  = "Done";
           // $this->load->view('edit_success_view', $data);
           return true;
     } else { return false;}
   }

but when i click on my delete link my link not disappear and record not deleted in database...! without codeigniter in simple solution its working fine...! please help me how its fixed and working properly...!
#2

[eluser]cideveloper[/eluser]
Install firefox and firebug and use the console to see what is going on. It will show you what is being posted if anything as well as the result.

The last line in your view is really butchered. Doesn't make sense. Also as a courtesy, when you post code please clean it up a bit so it is easy to read. For a question like this, it is not necessary to post any of your css.

Now to your code. Send the id as the third segment of your url as a jquery get and then have your controller process it as below. Do not use $_POST['id']

Code:
public function deleteSMS($id) {
    $this->db->where('sms_id', $id);
    $this->db->limit(1,0);
    $this->db->delete('sms');
    if ($this->db->affected_rows() > 0)
       return true;
    
    return false;
}
#3

[eluser]Ahmed Iqbal[/eluser]
thanks for reply i understand your controller code but what i can do with this code? please explain how pickup 'id' and transfer to controller Sad
Code:
$.ajax({
   type: "POST",
   url: "&lt;?=base_url();?&gt;index.php/admin/deleteSMS",
   data: dataString,
   cache: false,

   beforeSend: function()
   {
   parent.animate({'backgroundColor':'#fb6c6c'},300).animate({ opacity: 0.35 }, "slow");;
   },
   success: function()
   {
  
    parent.slideUp('slow', function() {$(this).remove();});
    
  }
  
});
#4

[eluser]cideveloper[/eluser]
Code:
$(function() {
$(".delete_button").click(function() {
var id = $(this).attr("id");
var parent = $(this).parent();
    
$.ajax({
   type: "GET",
   url: "&lt;?=base_url();?&gt;index.php/admin/deleteSMS/"+id,
   cache: false,

   beforeSend: function()
   {
   parent.animate({'backgroundColor':'#fb6c6c'},300).animate({ opacity: 0.35 }, "slow");;
   },
   success: function()
   {
  
    parent.slideUp('slow', function() {$(this).remove();});
    
  }
  
});
#5

[eluser]Ahmed Iqbal[/eluser]
Hello friend, thanks for help..! i've used your jquery/ajax code and i've changed my controller code look like this.
Code:
public function deleteSMS() {
    $this->db->where('sms_id', $this->uri->segment(3));
    $this->db->limit(1,0);
    $this->db->delete('sms');
    if ($this->db->affected_rows() > 0) return true;
    else return false;
}

my firebug display error look like this.
Code:
GET http://localhost/sitename/index.php/admin/deleteSMS/25111?_=1296899560300404 Not Found26ms
note: 25111 is id of record
#6

[eluser]cideveloper[/eluser]
I think "cache: false" is appending "?_=1296899560300404" to the end of the url. REmove the line "cache: false" from the ajax request.

Does the page work when you just put "http://localhost/sitename/index.php/admin/deleteSMS/25111" into the address bar?


Code:
$(function() {

    $(".delete_button").click(function(e) {
        e.preventDefault();
        var id = $(this).attr("id");
        var parent = $(this).parent();
        parent.animate({'backgroundColor':'#fb6c6c'},300).animate({ opacity: 0.35 }, "slow");

        $.ajax({
            url: "&lt;?=base_url();?&gt;index.php/admin/deleteSMS/"+id,
            success: function(){
                parent.slideUp('slow', function() {
                    $(this).remove();// I dont think this line will work in your code. $(this) is not the ajax object not the initial object above.
                });
            }
        });

    });

});
#7

[eluser]Ahmed Iqbal[/eluser]
wow thanks, you are right when i removed cache: false my script working properly...! great Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB