qty
ajax を使用してデータベースのフィールドを更新する際に問題が発生しています。マイナスボタンをクリックしようとすると、何度も減算し続けるため、エラーが発生した場所がわかりません。私は同じ問題をグーグルで検索しましたが、解決策が見つかりませんでした。これが私のコードです。
これが私のコントローラーです:
public function deduct(){
$this->inpatient_model->deduct();
$data['inpatient'] = $this->inpatient_model->getinpatientlab();
$this->load->view('admin/queryinpatient',$data);
}
ここに私のモデルがあります:
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');
}
}
}
これが私の見解です:
<?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;
});
})()
ここにあるものも、bootstrap- modalView
内で ajax を介してロードされます。なぜ何度も減算し続けるのか、私には本当にわかりません。何か助けはありますか?