开发者

Am i passing the value correctly to my controller from jquery?

开发者 https://www.devze.com 2023-01-15 20:51 出处:网络
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?\"))

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/libraryd开发者_StackOverflow社区elete' ?>",
                   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


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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消