我需要在单击提交按钮时插入多行。并且有一行并添加按钮。当单击添加按钮时,它将添加另一行。现在我需要通过单击提交按钮一次提交所有行值
根据上图,出现错误。然后当我点击添加按钮数据时插入数据库。但我以前只为添加空字段集添加按钮。不是提交表格。我只有一个项目名称的下拉列表。现在我想插入所有行的项目ID。任何人都可以帮助我。谢谢。我的代码如下。
查看
<!--form open-->
<form class="form-inline" role="form" id="frmadd" action="<?php echo base_url() ?>index.php/boq_controller/create" method="POST">
<!--project-->
<span><b>Project Name</b></span>
<?php
$attributes = 'class = "form-control" id = "project" style="width:100%; height:35px;"';
echo form_dropdown('project[]',$project, set_value('project'), $attributes);?>
<!--project-->
<!-- boq_tbl // field list-->
<table class="table" id="boq_tbl">
<thead>
<th>Item No</th>
<th>Description</th>
<th>Qty</th>
<th>Unit</th>
<th>Rate</th>
<th>Laboure Hrs</th>
<th>Labour Cost</th>
<th>Amount</th>
<thead>
<tbody>
<form class="form-inline" role="form" id="frmadd" action="<?php echo base_url() ?>index.php/boq_controller/create" method="POST">
<tr class="txtMult">
<td><input type="text" name="work_product_id[]" class="form-control" id="work_product_id" placeholder="" style="width:60px;" ></td>
<td><input type="text" name="work_item_description[]" class="form-control" id="work_item_description" placeholder="" style="width:350px;"></td>
<td><input type="text" name="quantity[]" id="" class="form-control val1" style="width:80px;" /></td>
<td><select style=" height: 28px; width: 70px; border-radius: 2px;" name="unit[]" >
<option value="" selected> </option>
<option value="cube">cube</option>
<option value="sq.ft">sq.ft</option>
<option value="Cwts">Cwts</option>
<option value="Gal" >Gal</option>
</select></td>
<td><input type="text" name="rate[]" class="form-control val2" style="width:80px;"/></td>
<td><input type="text" name="laboure_hrs[]" id="" class="form-control val3" style="width:80px;"/></td>
<td><input type="text"name="laboure_cost[]" id="" class="form-control val4" style="width:80px;"/></td>
<td>
<input type="text" id="txtmultTotal" name="txtmultTotal[]" style="width:80px;" placeholder="0.00" class="multTotal">
<!-- <input type="text" class="multTotal" placeholder="0.00">-->
</td>
<td><?php
//hidden value user session id
echo form_hidden('staff_id[]',($this->session->userdata['logged_in']['id']) );
?> </td>
</tr>
</tbody>
</table>
<p align="right">
Grand Total# <span id="grandTotal">0.00</span> <input type="text" id="txtgrandTotal" name="txtgrandTotal">
<button id="insert-more" class="btn btn-primary" type="">ADD</button>
</p>
<div class="form-group">
<input type="submit" class="btn btn-success" id="btn btn-success" value="submit">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!--/.container-->
</div>
<!--/.main-inner-->
</div>
<!--/.main-->
<script>
//add rows
$("#insert-more").click(function () {
$("#boq_tbl").each(function () {
var tds = '<tr class="txtMult">';
jQuery.each($('tr:last td', this), function () {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
$("#boq_tbl").on("keyup", ".txtMult input", multInputs);
//function for get total and grand total
function multInputs() {
var mult = 0;
$("tr.txtMult").each(function () {
var val1 = $('.val1', this).val();
var val2 = $('.val2', this).val();
var val3 = $('.val3', this).val();
var val4 = $('.val4', this).val();
var total = (val1 * val2 ) + (val3 * val4);
$('.multTotal',this).text(total);
$('#txtmultTotal',this).val(total);
mult += total;
});
$("#grandTotal").text(mult);
$('#txtgrandTotal').val(mult);
}
</script>
<script>
$(document).ready(function (){
//fill data
var btnedit='';
var btndelete = '';
fillgrid();
// add data
$("#frmadd").submit(function (e){
e.preventDefault();
$("#loader").show();
var url = $(this).attr('action');
var data = $(this).serialize();
$.ajax({
url:url,
type:'POST',
data:data
}).done(function (data){
$("#response").html(data);
$("#loader").hide();
fillgrid();
});
});
function fillgrid(){
$("#loader").show();
$.ajax({
url:'<?php echo base_url() ?>index.php/boq_controller/fillgrid',
type:'GET'
}).done(function (data){
$("#fillgrid").html(data);
$("#loader").hide();
btnedit = $("#fillgrid .btnedit");
btndelete = $("#fillgrid .btndelete");
var deleteurl = btndelete.attr('href');
var editurl = btnedit.attr('href');
//delete record
btndelete.on('click', function (e){
e.preventDefault();
var deleteid = $(this).data('id');
if(confirm("are you sure")){
$("#loader").show();
$.ajax({
url:deleteurl,
type:'POST' ,
data:'id='+deleteid
}).done(function (data){
alert(deleteurl);
$("#response").html(data);
$("#loader").hide();
fillgrid();
});
}
});
});
}
});
</script>
控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Boq_controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('boq_model');
$this->load->library('form_validation');
}
// load boq page
public function index()
{
$data['project'] = $this-> boq_model ->get_project();
$this -> load -> view('include/header');
$this->load->view('boq/boq',$data);
}
// load boq page
public function create(){
// validate fields
$this->form_validation->set_rules('work_product_id', 'Work Product Id', 'required');
$this->form_validation->set_rules('work_item_description', 'Work Item Description', 'required');
$this->form_validation->set_rules('quantity', 'Quantity', 'required');
$this->form_validation->set_rules('rate', 'Rate', 'required|numeric');
$this->form_validation->set_rules('laboure_hrs', 'Laboure Hrs', 'required|numeric');
$this->form_validation->set_rules('laboure_cost', 'Laboure Cost', 'required|numeric');
if ($_POST)
{
$project_id=$this->input->post('project');
$staff_id=$this->input->post('staff_id');
$item_no=$this->input->post('work_product_id');
$description=$this->input->post('work_item_description');
$qty=$this->input->post('quantity');
$unit=$this->input->post('unit');
$rate=$this->input->post('rate');
$laboure_hrs=$this->input->post('laboure_hrs');
$laboure_cost=$this->input->post('laboure_cost');
$amount=$this->input->post('txtmultTotal');
$data = array();
for ($i = 0; $i < count($this->input->post('work_product_id')); $i++)
{
$data[$i] = array(
'project_id' => $project_id[$i],
'staff_id' => $staff_id[$i],
'item_no' => $item_no[$i],
'description' => $description[$i],
'qty' => $qty[$i],
'unit' => $unit[$i],
'rate' => $rate[$i],
'laboure_hrs' => $laboure_hrs[$i],
'laboure_cost' => $laboure_cost[$i],
'amount' => $amount[$i],
);
}
print_r($data);
$this->boq_model->create($data);
}
}
}
模型
class Boq_model extends CI_Model {
//Get project name
function get_project()
{
$this->db->select('id');
$this->db->select('project_name');
$this->db->from('project');
$query = $this->db->get();
$result = $query->result();
$project_id = array('-SELECT PROJECT NAME -');
$project_name = array('-SELECT PROJECT NAME-');
for ($i = 0; $i < count($result); $i++)
{
array_push($project_id, $result[$i]->id);
array_push($project_name, $result[$i]->project_name);
}
return $project_result = array_combine($project_id, $project_name);
}
function create($data) {
$this->db->insert_batch('boq', $data);
}
}