简体   繁体   English

使用带有Codeigniter活动记录的Ajax从数据库更新字段失败

[英]Updating a field from database using ajax with codeigniter active record fails

I'm having a problem in updating the qty field in my database using ajax. 我在使用ajax更新数据库中的qty字段时遇到问题。 I don't know where the error occur because when I tried to click the minus button it keeps on subtracting it multiple times. 我不知道错误发生在哪里,因为当我尝试单击减号按钮时,它会连续多次减去它。 I've google same problem but didn't find any solution. 我有谷歌相同的问题,但没有找到任何解决方案。 Here is my code. 这是我的代码。

Here is my CONTROLLER: 这是我的控制器:

 public function deduct(){
    $this->inpatient_model->deduct();
    $data['inpatient'] = $this->inpatient_model->getinpatientlab();
    $this->load->view('admin/queryinpatient',$data);
 }

Here is my MODEL: 这是我的模型:

 public function deduct(){  
    $this->db->select('*');
    $this->db->from('inpatientlab');
    $this->db->where('ilid',$this->input->post('lid'));
    $query = $this->db->get();
    $row = $query->result_array();
    if($query->num_rows() > 0 ){
        if($row[0]['qty'] > 1){
            $uno = 1;
            $this->db->set('qty','qty-'.$uno,FALSE)
                    ->where('ilid',$this->input->post('lid'))
                    ->update('inpatientlab');               
        }
    }   

}

Here is my VIEW: 这是我的观点:

<?php if($inpatient): ?>
<table class="table table-striped">
    <tr>
        <th>Description</th>            
        <th>Quantity</th>
        <th>Price</th>
        <th>Sub Total</th>
        <th>Action</th>
    </tr>
<?php foreach($inpatient as $rows): ?>
    <tr>
        <td><?php echo $rows->ldesc; ?></td>
        <td><?php echo $rows->qty; ?></td>
        <td><?php echo $rows->lprice; ?></td>
        <td><?php echo number_format($rows->qty * $rows->lprice,2); ?></td>
        <td>
            <button value="<?php echo $rows->ilid; ?>" class="btn btn-danger btnRemove"><i class="icon-trash"></i></button>
            <button value="<?php echo $rows->ilid; ?>" class="btn btn-danger btnMinus"><i class="icon-minus"></i></button>
        </td>
    </tr>
<?php endforeach; ?>
</table>
<script type="text/javascript" >
    (function(){        
    $(document).on('click','.btnMinus',function(){
        var lid = $(this).val(),
            pid = $('.pid').val(),
            dataString = "id=" + pid + "&lid=" + lid;           
            console.log(dataString);
            $.ajax({
                type:"POST",
                url: base_url + "inpatient/deduct",
                data:dataString,
                success:function(data){
                    $('.pickedlab').html(data);
                }
            })
        return false;

    });

})()

My View in here is also loaded via ajax inside a bootstrap-modal. 此处的My View也通过Ajax加载到bootstrap-modal中。 I really have no idea why it keeps on subtracting multiple times. 我真的不知道为什么它会不断减少多次。 Any help? 有什么帮助吗?

Have you tried using the browser debug and find out whether your AJAX is calling multiple times. 您是否尝试过使用浏览器调试,并找出您的AJAX是否正在多次调用。 I am not familiar with your Controller-Model settings, normally my models are pure POCO class only. 我不熟悉您的Controller-Model设置,通常我的模型仅是纯POCO类。 All computations are done at the controller level. 所有计算均在控制器级别完成。

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

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