Welcome Guest, Not a member yet? Register   Sign In
How to delete records from DB using $_POST method
#10

[eluser]Cristian Gilè[/eluser]
HTML does not directly support such a feature. You need javascript. Try something like this:

delete link in the view:


Code:
<a href="#" class="delete" value="&lt;?=$record-&gt;id?&gt;">delete</a>

javascript code:

Code:
$(document).ready(function()
{
      $(".delete").live('click', function(e)
      {
        e.preventDefault();

        var record_id = $(this).attr('value');

        $.ajax({  
             type: "POST",  
             url: "&lt;?php echo site_url('location/delete');?&gt;",  
             data: "delete="+ record_id,
             dataType: 'json',  
             success: function(res){  
                  var obj = eval(res);
                  alert(obj.message);
             }
         });  
      });    
});

In your controller, after a successful delete operation, return a json var like this:

in your controller: (location/delete)

Code:
function delete()
{
     if(empty($_POST))
     {
        //redirect if no var was post
     }
    
     if($id = $this->input->post('delete'))
     {
         //if delete was successful
         $data['message'] = 'Record '.$id.' was deleted successfully';
         //else
         $data['message'] = 'An error occurred during the delete operation';

         echo json_encode($data)
     }

     ....
     ....
}


Cristian Gilè


Messages In This Thread
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 08:27 AM
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 09:25 AM
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 09:29 AM
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 09:42 AM
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 09:44 AM
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 09:47 AM
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 09:48 AM
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 09:51 AM
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 10:00 AM
How to delete records from DB using $_POST method - by El Forum - 01-26-2011, 10:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB