动态插入复数行的取值问题
现在困扰的问题。使用,PHP,SQL Server2008 ,做了一个登录系统。
「行挿入」按键按后,自动追加2行,2行为一组。(如图 1-1,1-2 是第一组,2-1,2-2是第二组)
「登録」按键按后,把客户端填写的东西都输入到DB。
可是,那个Table的取值是动态插入的,不会取值。
别人说,把js 动态的创建tr td 和input,然后php可以直接遍历
$_GET或者$_POST,数组就能取得所有值。。
综上所述,从做的这个系统来看,主要是想 在动态的Table的每2组行中,取出输入值,然后带入DB。
-------------------------------------------------------
主要的代码为下所述。
行插入 按键按下时的Javascript
///代码省略
<script type="text/javascript">
// 行挿入
function AddTableRows2(){
counter++;
var newTR;
var newTD;
//項目行-1(1-1)---------------------------
var HTML1 = '<th color="ffa500" size="5">行</th>';
var HTML2 = '<th color="ffa500" >商品-(規格1/規格2)-(在庫)</th>';
var HTML3 = '<th color="ffa500" >箱数/数量</th>';
var HTML4 = '<th color="ffa500" >単位</th>';
var HTML5 = '<th color="ffa500" >仕入単価</th>';
var HTML6 = '<th color="ffa500" >仕入金額</th>';
newTR = data.insertRow();
newTR.setAttribute("align","left");
newTR.setAttribute("valign","middle");
newTD = newTR.insertCell();
newTD.innerHTML = HTML1;
newTD = newTR.insertCell();
newTD.innerHTML = HTML2;
newTD = newTR.insertCell();
newTD.innerHTML = HTML3;
newTD = newTR.insertCell();
newTD.innerHTML = HTML4;
newTD = newTR.insertCell();
newTD.innerHTML = HTML5;
newTD = newTR.insertCell();
newTD.innerHTML = HTML6;
var HTML1 = '' + counter + '-1';
var HTML2 = '<input type="text" name="itemName_lot' + counter + '" value="" size="30" maxlength="20" /> <input type="button" name="bt_busyo" value="Select" onclick="JavaScript:winopen_itemName_lot();" >';//</td>
//var HTML2 = '<input type="text" name="itemName_lot' + counter + '" value="" size="35" maxlength="20" /> <input type="button" name="bt_busyo" value="Select" onclick="JavaScript:winopen_itemName_lot' + counter + '"();" >';//</td>
var HTML3 = '<input type="text" name="boxSum' + counter + '" value="" size="10" maxlength="20" />';
var HTML4 = '<input type="text" name="unit' + counter + '" value="" size="10" maxlength="20" />';
var HTML5 = '<input type="text" name="shiiretanka' + counter + '" value="" size="10" maxlength="20" />';
var HTML6 = '<input type="text" name="shiirekingaku' + counter + '" value="" size="10" maxlength="20" />';
//<input type="radio" name="radiobutton" value="'.$counter.'">
//2012.3.5 add 配列
//行1---------------------------
newTR = data.insertRow();
newTR.setAttribute("align","left");
newTR.setAttribute("valign","middle");
/*for(var i=0;i<4;i++){
newTD = newTR.insertCell();
newTD.innerHTML = "111";
}*/
newTD = newTR.insertCell();
newTD.innerHTML = HTML1;
newTD = newTR.insertCell();
newTD.innerHTML = HTML2;
newTD = newTR.insertCell();
newTD.innerHTML = HTML3;
newTD = newTR.insertCell();
newTD.innerHTML = HTML4;
newTD = newTR.insertCell();
newTD.innerHTML = HTML5;
newTD = newTR.insertCell();
newTD.innerHTML = HTML6;
//項目行2------------------------------
var HTML1 = '<th color="ffa500" > </th>';
var HTML2 = '<th color="ffa500" >備考</th>';
var HTML3 = '<th color="ffa500" >コスト</th>';
var HTML4 = '<th color="ffa500" >販売</th>';
var HTML5 = '<th color="ffa500" >賞味期限</th>';
var HTML6 = '<th color="ffa500" >killing</th>';
newTR = data.insertRow();
newTR.setAttribute("align","left");
newTR.setAttribute("valign","middle");
/*for(var i=0;i<4;i++){
newTD = newTR.insertCell();
newTD.innerHTML = "111";
}*/
newTD = newTR.insertCell();
newTD.innerHTML = HTML1;
newTD = newTR.insertCell();
newTD.innerHTML = HTML2;
newTD = newTR.insertCell();
newTD.innerHTML = HTML3;
newTD = newTR.insertCell();
newTD.innerHTML = HTML4;
newTD = newTR.insertCell();
newTD.innerHTML = HTML5;
newTD = newTR.insertCell();
newTD.innerHTML = HTML6;
//行2------------------------------
var HTML1 = '' + counter + '-2';
var HTML2 = '<input type="text" name="sub2bikou' + counter + '" value="" size="30" maxlength="20" />';//</td>
var HTML3 = '<input type="text" name="cost' + counter + '" value="" size="10" maxlength="20" />';
var HTML4 = '<input type="text" name="sales' + counter + '" value="" size="10" maxlength="20" />';
var HTML5 = '<input type="text" name="syoumikigen' + counter + '" value="" size="10" maxlength="20" />';
//var HTML6 = '<input type="text" name="killing' + counter + '" value="" size="10" maxlength="20" />';
var HTML6 = "<div align='center' style='width:40px'><a href='javascript:;' onclick=\"DeleteSignRow('data" + counter + "')\">削除</a></div>";
newTR = data.insertRow();
newTR.setAttribute("align","left");
newTR.setAttribute("valign","middle");
/*for(var i=0;i<4;i++){
newTD = newTR.insertCell();
newTD.innerHTML = "111";
}*/
newTD = newTR.insertCell();
newTD.innerHTML = HTML1;
newTD = newTR.insertCell();
newTD.innerHTML = HTML2;
newTD = newTR.insertCell();
newTD.innerHTML = HTML3;
newTD = newTR.insertCell();
newTD.innerHTML = HTML4;
newTD = newTR.insertCell();
newTD.innerHTML = HTML5;
newTD = newTR.insertCell();
newTD.innerHTML = HTML6;
//最後の処理
newTR.onclick = setBGCOLOR;
}
//省略
?????HTML部分
<!-- タブの切り替え部分 -->
<div id="javascript_tab_sample">
<ul id="tab">
<li class="selected"><a href="#w3c">契約情報</a></li>
<!-- <li><a href="#xhtml">明細</a></li> <li><a href="#css">請掛</a></li>-->
</ul>
<dl id="w3c">
<!-- <dl id="xhtml">
<dt> タブ2</dt> -->
<dd>
<TABLE border="1" cellspacing=0 bordercolor="black" style="color:black" >
<!--<THEAD align="center"><TD>項1</TD><TD>項2</TD><TD>項3</TD><TD>項4</TD></THEAD>-->
<THEAD align="center">
<th>行</th>
<!--<th>商品/ロットNo.(規格1/規格2)(在庫)</th>
<th>箱数/????/数量</th>-->
<th>商品-(規格1/規格2)-(在庫)</th>
<th>箱数/数量</th>
<th>単位</th>
<th>仕入単価</th>
<th>仕入金額</th>
</THEAD>
<TBODY id="data">
<TR align="center" valign="middle" onclick="setBGCOLOR()">
<!--<TD>@</TD> <TD>@</TD> <TD>@</TD> <TD>@</TD> </TR>-->
</TBODY>
<!-- <table border="1" cellspacing="0" cellpadding="4" id="table1">
<tr>
<th>行</th>
<th>商品/ロットNo.-(規格1/規格2)-(在庫)</th>
<th>箱数/????/数量</th>
<th>単位</th>
<th>仕入単価</th>
<th>仕入金額</th>
</tr>
<tr>
<td class="line">0行</td>
<td class="itemName_lot"><input type="text" name="itemName_lot" value="" size="20" maxlength="20" />
<input type="button" name="bt_busyo" value="Select" onclick="JavaScript:winopen_itemName_lot();" ></td>
<td class="boxSum"><input type="text" name="boxSum" value="" size="10" maxlength="20" /></td>
<td class="unit"><input type="text" name="unit" value="" size="10" maxlength="20" /></td>
<td class="shiiretanka"><input type="text" name="shiiretanka" value="" size="10" maxlength="20" /></td>
<td class="shiirekingaku"><input type="text" name="shiirekingaku" value="" size="10" maxlength="20" /></td>
</tr>
</table>
<table border="1" cellspacing="0" cellpadding="4" id="table2"> </table>-->
<table>
<tbody>
</tbody>
</table>
<!--<input type="submit" name="bt_loading" value="????????取込" class="button">
<input type="submit" name="bt_excelOut" value="Excel書出" class="button">
<input type="submit" name="bt_excelInput" value="Excel取込" class="button">-->
移動先明細番号<input type="text" name="targetID" value="" size="5">
<input type="submit" name="bt_targetLine" value="行移動" class="button">
<!--<input type="submit" name="bt_costSmu" value="コスト計算" class="button">-->
<input type="submit" name="bt_sendou_sub2" value="先頭行" class="button">
<input type="submit" name="bt_saisyu_sub2" value="最終行" class="button">
<input type="button" name="bt_addLine_sub2" value="行追加" class="button" onClick="AddTableRows();">
<input type="button" name="bt_addInsert_sub2" value="行挿入" class="button" onClick="AddTableRows2();">
<input type="button" name="bt_addDel_sub2" value="行削除" class="button">
<INPUT TYPE ="button" name="tourroku" VALUE="実行" class="button" onClick="GetTextValue()">
</dd>
</dl>
</div>
[解决办法]
形如 <input type="text" name="itemName_lot' + counter + '" 的
改为形如 <input type="text" name="itemName_lot[' + counter + ']" 这样的
提交后可用形如 $_POST['itemName_lot'][$i] 的访问