简体   繁体   English

将动态值传递给onclick函数-CodeIgniter

[英]passing dynamic value to onclick function - CodeIgniter

I was trying to pass an id to onclick function but it returns wrong value when I used it on my view. 我试图将id传递给onclick函数,但是在视图上使用它时返回错误的值。

My function in my model that gets the ID in the database: 我的模型中的函数在数据库中获取ID:

public function _get_pr_id()
{
    $this->db->select('pr_id');
    $this->db->from('tblprequest');
    $this->db->order_by('pr_date_created', 'DESC');
    $this->db->limit(1);
    $query = $this->db->get();
    $row = $query->row();
    return $row->pr_id;
}

The purpose of this function is that it gets the latest pr_id inserted in the database. 此功能的目的是获取数据库中最新插入的pr_id Example is that '15'. 例如“ 15”。

And here is my Controller: 这是我的控制器:

public function purchase_request()
{
    if ($this->session->userdata('username'))
    {
        $data = array(
            'item' => $this->pr->_get_item(),
            'dept' => $this->pr->_get_department(),
            'pr_id' => $this->pr->_get_pr_id(),
            'admin_content' => 'pages/admin_purchase_request'
        );
        $this->load->view('admin_template/admin', $data);
    }
    else
    {
        redirect('login');
    }
}

I store the value of the function $this->pr->_get_pr_id() to a variable pr_id so that I can use it in my View with AJAX. 我将函数$this->pr->_get_pr_id()的值pr_id到变量pr_id以便可以在AJAX的View中使用它。

And here is the View: 这是视图:

<a onclick="finalize('<?php echo $pr_id; ?>')" id="finalize" class="btn btn-success" aria-expanded="false">Finalize <i class="fa fa-check"></i></a>

And my function in my View: 我在视图中的功能:

function finalize(id)
{
   alert(id);
}

Now, the problem: For example in my db, the latest pr_id is 15 but when I click the finalize button it says to me the pr_id is 14 . 现在是问题所在:例如,在我的数据库中,最新的pr_id15但是当我单击finalize按钮时,它告诉我pr_id14 Hope you understand my question. 希望你理解我的问题。 Thanks 谢谢

If your pr_id is auto-increment column then just order by your result using pr_id. 如果您的pr_id是自动递增列,则只需使用pr_id按结果排序即可。 Because ordering by integer column is fast. 因为按整数列排序是快速的。 And in your case there is a possibility of new record is inserted after you fetched the latest pr_id. 在您的情况下,获取最新的pr_id后可能会插入新记录。 So there is no fool-proof solution to handle your case. 因此,没有万无一失的解决方案来处理您的案件。 So, When you call finalize() , again check your current pr_id value is latest one. 因此,当您调用finalize() ,再次检查您当前的pr_id值是否为最新值。 Based on that you could handle your logic. 基于此,您可以处理您的逻辑。

I guess that you have to use ajax with the onclick event this way you are sure you get the last value from database, in the use case you described, there is another source that updated the database after you grab the data, so using ajax will provide the last stored value, you can use something similar to : 我想您必须将ajax与onclick事件一起使用,这样才能确保您从数据库中获取了最后一个值,在您描述的用例中,有另一个来源在获取数据后会更新数据库,因此使用ajax可以提供最后的存储值,您可以使用类似于以下内容的东西:

$(document).on("click",".yourselector",function(e){

$.ajax({
    type: 'POST',
    url: "controller/function",
    data: {
        param: myparamvalue
    },
    beforeSend: function() {

    },
    complete: function() {

    },
    success: function(_result) {

    }
});

});

where Controller is the controller that call the model using the method to grab the id from db 其中Controller是使用该方法从db获取ID的模型调用控制器的控制器

if not working plz replace : 如果不起作用请替换:

controller/function

by this : 这样 :

/controller/function

Hoe it helps, helps头有帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM