views:

54

answers:

4

I am doing a delete on a table using jquery,

$('table#chkbox td a.delete').click(function()
    {
        if (confirm("Are you sure you want to delete this row?"))
        {
            var id = $(this).parent().attr('id');

            var parent = $(this).parent().parent();

            $.ajax(
            {
                   type: "POST",
                   url: "<?php echo base_url().'index.php/libraryController/librarydelete' ?>",
                   data: { 'id': id },
                   cache: false,

                   success: function()
                   {
                        parent.fadeOut('slow', function() {$(this).remove();});
                   }
             });                
        }
    });

I am getting the id value correctly but my data parameter doesn't get passed to my controller,

function librarydelete($id)
{
$del = $id;
echo $del; 
$this->librarymodel->deletebook_issue($id);
$this->session->set_flashdata('response','Deleted successfully');
redirect('libraryController/loadmagazinedetails');
}

Any suggestion...

I am getting the error,

Missing argument 1 for libraryController::librarydelete() and Undefined variable: id

A: 

Change id to $id in controller function parameter means, your function should be

function librarydelete($id)
{
    $del = $id;
    echo $del; 
    $this->librarymodel->deletebook_issue($id);
    $this->session->set_flashdata('response','Deleted successfully');
    redirect('libraryController/loadmagazinedetails');
}
Yogesh
@Yogesh echo didn't get displayed...
chandru_cp
so it means that parameter is not being passed to your function. so as suggested above use $_POST directly to fetch your parameter. This is the only change u will need to do.
Yogesh
A: 

Perhaps it is because you are using the variable name 'data' twice. Try renaming var data to var mydata.

MattSmith
@MAtt i changed still i cant see the echoed variable id in my controller function..
chandru_cp
A: 

Hi,

The way you are passing data is wrong. This is the way you should be setting data. var data= {id: id };

in your case let me not use the variable in the post action and show you how its done.

$.ajax(
            {
                   type: "POST",
                   url: "<?php echo base_url().'index.php/libraryController/librarydelete' ?>",
                   data: {id:id},
                   cache: false,

                   success: function()
                   {
                        parent.fadeOut('slow', function() {$(this).remove();});
                   }
             });     

Edit: After you modified your code to pass data the right way.

Hey I have not followed MVC framwework in php. but since you are doing a post on the action you can always get the data the following way:

$id=$_POST['id']

and then you can use this id.

Also if you still want to use the function:

function librarydelete($id)
{
$del = $id;
echo $del; 
$this->librarymodel->deletebook_issue($id);
$this->session->set_flashdata('response','Deleted successfully');
redirect('libraryController/loadmagazinedetails');
}

just modify the uri action to existingurl+/ i.e append the id to the end and in your route map (if there exists something like this in php in .nets implementation there is 1) just add that route.

+2  A: 

Your are posting the data, so you can get the id like this:

function librarydelete()
{
    $del = $_POST['id'];
    echo $del; 
    $this->librarymodel->deletebook_issue($_POST['id']);
    $this->session->set_flashdata('response','Deleted successfully');
    redirect('libraryController/loadmagazinedetails');
}

Looks like you are using codeigniter, so this would be even better:

$del = $this->input->post('id');
captaintokyo
captaintokyo its working..........
chandru_cp
Great. The way you were trying to do it before only works if you do a GET request and use Codeigniter's built in support for uri segments.
captaintokyo