我是否将值从 jquery 正确传递到我的控制器?

发布于 2024-09-18 20:54:34 字数 1156 浏览 1 评论 0原文

我正在使用 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();});
                   }
             });                
        }
    });

我正确获取了 id 值,但我的数据参数没有传递到我的控制器,

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

任何建议...

我收到错误,

缺少 LibraryController 的参数 1 ::librarydelete() 和 未定义变量:id

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

望她远 2024-09-25 20:54:34

您正在发布数据,因此您可以像这样获取 id:

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

看起来您正在使用 codeigniter,所以这会更好:

$del = $this->input->post('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');
嘦怹 2024-09-25 20:54:34

将控制器功能参数中的 id 更改为 $id
意味着,你的函数应该是

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

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');
}
初心未许 2024-09-25 20:54:34

也许是因为您使用了变量名称“data”两次。尝试将 var data 重命名为 var mydata。

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

呢古 2024-09-25 20:54:34

您传递数据的方式是错误的。这就是您应该设置数据的方式。
var data= {id: id };

在你的情况下,让我不要在后期操作中使用变量并向你展示它是如何完成的。

$.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();});
                   }
             });     

编辑:修改代码以正确方式传递数据后。

嘿,我没有遵循 php 中的 MVC 框架。但由于您正在对操作进行发布,因此您始终可以通过以下方式获取数据:

$id=$_POST['id']

然后您可以使用此 id。

另外,如果您仍然想使用该功能:

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

只需将uri操作修改为existingurl+/即,将id附加到末尾和您的路由映射中(如果在.nets实现中的php中存在类似的东西,则有1)只需添加路线。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文