我有一个插入循环数据的表单。问题是即使我没有在我的表单上插入新数据,我的数据库仍然添加一个新的。我可以在同一个数据中添加更多的一个数据,但是在第一个之后创建新数据时,得到两个数据为空值。 这是我的代码
控制器
$doc_number = $_POST['doc_number'];
$closedby = $_POST['closedby'];
$reasonclose = $_POST['reasonclose'];
$description = $_POST['description'];
for($i=0;$i<count($closedby);$i++) {
$solved = array(
'DOC_NUMBER' => $doc_number,
'LOB' => $closedby,
'REASON' => $reasonclose,
'DESCRIPTION' => $description,
'CREATE_DATE' => $dateop,
'ID_SOLVED' => ++$num
);
$this->db->insert('NEWS_EVENT_SOLVED_TAB',$solved);
var_dump($solved);
}
查看
<div class="controls" style="margin-left: 70px;">
<input id="idf" type="hidden" value="<?php echo $num;?>" />
<input id="idf" type="hidden" value="1" />
<div id="divSolved"></div>
<button type="button" onclick="addLob(); return false;">Add LOB</button>
</div>
JAVASCRIPT
function addLob() {
var idf = $("#idf").val();
var stre = "<p id='srow" + idf +"'>"+
"<select id='closedby_"+idf+"' name='closedby[]' class='form-control span3'>"+
"<option value=''>Choose One LOB</option>"+
"<option value='alkes'>Alkes</option>"+
"<option value='bd'>BD</option>"+
"<option value='cosmetic'>Cosmetic</option>"+
"<option value='feed'>Feed</option>"+
"<option value='food'>Food</option>"+
"<option value='import'>Import</option>"+
"<option value='logistic'>Logistic</option>"+
"<option value='pharma'>Pharma</option>"+
"<option value='purchasing'>Purchasing</option>"+
"<option value='qa'>QA</option>"+
"</select>"+
"<select id='reasonclose_"+idf+"' name='reasonclose' class='form-control span3'>"+
"<option value=''>Choose One Reason</option>"+
"<option value='Claim Insurance'>Claim Insurance</option>"+
"<option value='Claim Principal'>Claim Principal</option>"+
"<option value='Claim Transporter'>Claim Transporter</option>"+
"<option value='Sample'>Sample</option>"+
"<option value='Sold To Customer'>Sold To Customer</option>"+
"</select>"+
"<textarea rows='2' id='description_"+idf+"' name='description' placeholder='Keterangan'/>"+
"<a href='#' style='color:#3399FD;' onclick='hapusElemen(\"#srow" + idf + "\"); return false;'><img alt='' src='<?php echo themeUrl(); ?>images/delete.png'></a>"+
"</p>";
$("#divSolved").append(stre);
idf++;
$("#idf").val(idf);
}
function hapusElemen(idf) {
$(idf).remove();
}
function confirm(){
var isi = '<div align="" class="text-">Please wait while processing...<br /><img alt="" src="<?php echo themeUrl(); ?>img/ajax_loader.gif" align=""></div>';
$('.modal-body').html(isi);
var modalWidth = 300;
var left = (screen.availWidth - (modalWidth * 2));
$('button.close').hide();
$('#myModal').css({'width':modalWidth+'px','left':left+'px'}).modal({backdrop:'static',keyboard:false,show:true});
var hotdata = $('#productclose').handsontable('getInstance');
hstable = JSON.stringify({'data':hotdata.getData()});
//alert(lob);
for(var i=1; i<$('#idf').val(); i++) {
$.post('<?php echo base_url('report/news_event/confirmed'); ?>',{
doc_number : $('#id_number').val(),
closedby : $('#closedby_'+i).val(),
reasonclose : $('#reasonclose_'+i).val(),
description : $('#description_'+i).val(),
itemdata : hstable
//datenumber : $('#reminder_date').val()
},function() {
//alert($('#closedby').val());
//location.reload();
});
}
}
答案 0 :(得分:0)
您遇到的问题是,当您提交时,即使没有添加内容,仍然会有一条$_POST['closedby']
条记录,但该值可能为空白。
在for循环中,您应该检查$_POST['closedby']
是否为空/空白,或者通常是选项的默认值。如果是,那么就不要插入。类似的东西:
if (!empty($_POST['closedby'][$i])
{
// insert code
}
作为旁注,您应该使用模型与数据库进行交互,并在模型中使用控制器调用函数。