我已經使用ajax調用完成數據插入。當點擊提交按鈕時,它將重定向到查看頁面。但問題是,當連續點擊提交按鈕時,多個條目會被傳送到database.how。我給我的代碼below.please幫助。如何避免在雙擊按鈕上插入多個數據
控制器
public function addmerchant() {
$this->load->helper(array('form', 'url'));
$this->load->helper('file');
$ip = $_SERVER['REMOTE_ADDR'];
$config['upload_path'] = 'assets/images/merchantquote';
$config['allowed_types'] ='gif|jpg|jpeg|png|pdf|doc|docx|txt';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['width'] = 75;
$config['height'] = 50;
if (isset($_FILES['txtattachments']['name'])) {
$filename = "-" . $_FILES['txtattachments']['name'];
$config['file_name'] = substr(md5(time()), 0, 28) . $filename;
}
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$field_name = "txtattachments";
$this->load->library('upload', $config);
if (!$this->upload->do_upload('txtattachments')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat6 = array('upload_data' => $this->upload->data());
$this->resize($dat6['upload_data']['full_path'], $dat6['upload_data']['file_name']);
}
if (empty($dat6['upload_data']['file_name'])) {
$merimage = '';
} else {
$merimage = $dat6['upload_data']['file_name'];
}
$data7 = array(
'qub_merchantid' => $this->input->post('merchant'),
'qub_customerid' => $this->input->post('customer'),
'qub_shipcontactname' => $this->input->post('txtShipContactName'),
'qub_shipaddress' => $this->input->post('txtAddress'),
'qub_shiplocation'=>$this->input->post('txtLocation'),
'qub_telephone'=>$this->input->post('txtTelephone'),
'qub_mobile'=>$this->input->post('txtMobile'),
'qub_email'=>$this->input->post('txtEmail'),
'qub_payterms'=>$this->input->post('txtPayterms'),
'qub_paymode'=>$this->input->post('txtPaymode'),
'qub_deliveryperiod'=>$this->input->post('txtDelivery'),
'qub_incoterms'=>$this->input->post('txtIncoterms'),
'qub_attachment'=>$merimage,
'qub_expiry'=>$this->input->post('txtExpiry'),
'qub_permission'=>0,
'qub_dated'=>time(),
'qub_ipadd'=>$ip
);
$inserted_id = $this->quotemerchant_model->forminsert($data7);
$unitprice = $this->input->post('txtUnitprice');
$quantity = $this->input->post('txtQuantity');
$discount = $this->input->post('txtDiscount');
$totalprice = $quantity * $unitprice;
$effective = $totalprice - $discount;
$vat = $this->input->post('txtVAT');
$gst = $this->input->post('txtGST');
$service = $this->input->post('txtServicetax');
$octroi = $this->input->post('txtOctroi');
$excise = $this->input->post('txtExcise');
$shipping = $this->input->post('txtShipping');
$other = $this->input->post('txtOthertaxcharges');
$dats['gstamount'] = ($effective * $gst)/100;
$dats['servicetaxamt'] = ($effective * $service)/100;
$dats['octroiamt'] = ($effective * $octroi)/100;
$dats['exciseamt'] = ($effective*$excise)/100;
$dats['vatamt'] = ($effective + $dats['exciseamt']) * $vat/100;
$dats['netamount'] = $effective + $dats['gstamount'] + $dats['octroiamt'] + $dats['servicetaxamt'] + $dats['exciseamt'] + $other + $dats['vatamt']+ $shipping;
$sellingprice= $dats['netamount'];
$data8=array(
'qud_qubid' => $inserted_id,
'qud_productid' => $this->input->post('sbProduct'),
'qud_unit' => $this->input->post('sbUnit'),
'qud_quantity' => $this->input->post('txtQuantity'),
'qud_unitprice'=>$this->input->post('txtUnitprice'),
'qud_vat'=>$this->input->post('txtVAT'),
'qud_gst'=>$this->input->post('txtGST'),
'qud_servicetax'=>$this->input->post('txtServicetax'),
'qud_octroi'=>$this->input->post('txtOctroi'),
'qud_excise'=>$this->input->post('txtExcise'),
'qud_othertaxcharges'=>$this->input->post('txtOthertaxcharges'),
'qud_shipping'=>$this->input->post('txtShipping'),
'qud_discount'=>$this->input->post('txtDiscount'),
'qud_totalprice'=>$totalprice,
'qud_sellingprice'=>$sellingprice,
'qud_requirementid'=>$this->input->post('txtRequirement'),
'qud_rfqid'=>$this->input->post('txtRFQid'),
'qud_enduser'=>$this->input->post('txtEnduser'),
'qud_merchantid'=>$this->input->post('merchant'),
'qud_customerid'=>$this->input->post('customer'),
'qud_dated'=>time(),
'qud_ipadd'=>$ip,
);
$admid= $this->session->userdata('moderId');
$revicedata=array(
'rev_processtype'=>'1',
'rev_actionlevel'=>'new',
'rev_processdataid' => $inserted_id,
'rev_admtype' =>2,
'rev_admid'=>$admid,
'rev_dated'=>time(),
'rev_ipadd'=>$ip,
);
// echo '<pre>'; print_r($this->session->all_userdata());exit;
$reviceinsert=$this->quotemerchant_model->insertreviceaction($revicedata);
$detail=$this->quotemerchant_model->forminsertdetail($data8);
$response=array('id'=>$inserted_id,'message'=>"inserted successfully");
echo json_encode($response);
die();
}
查看
<script type="text/javascript">
$('#merchantsubmit').click(function() {
var formData = new FormData($('#myform')[0]);
$.ajax({
url: "<?php echo base_url() ?>moderator/QuoteMerchant/addmerchant",
type: 'POST',
data: formData,
dataType:"Json",
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
success: function(data) {
var last_inserted_id = data.id;
window.location.href ="<?php echo base_url() ?>moderator/QuoteMerchant/viewmerchant/"+last_inserted_id;
// window.location.href ="<?php //echo base_url() ?>moderator/RFQ/viewrfq/"+ form_data.reqid;
// alert('added Successfully');
}
});
return false;
});
</script>
模型
public function forminsert($data7)
{
$this->db->insert('jil_quotemjbrief',$data7);
return $this->db->insert_id();
}
public function forminsertdetail($data8) {
$this->db->insert('jil_quotemjdetail',$data8);
return $this->db->insert_id();
}
你可以重新載入你的頁面後ajax成功或重置表格 –
它是如何工作的?你可以解釋嗎? – Angel
成功後數據插入你顯示成功消息或任何其他事情顯示 –