2013-03-12 20 views
1

我使用動態表創建了一個簡單的表單。這個表格行按用戶輸入值遞增,因此每次創建新行時都需要增加組合框或選擇(選項),所以它應該像默認的組合框值一樣。注意:此選擇或組合框是列屬性的一部分,請記住行增量,但不包含列。如何收集動態添加行(HTML表)的值(數據)?使用php&XAMPP

第二個問題:

我要收集此表列中的數據或值不論其行數爲,並且使用PHP其存儲到數據庫XAMPP。我知道如何從單一表單收集數據,但我不知道如何從動態表單收集數據。

下面是代碼:

function AddRows() 
{ 
var tableelement = document.getElementById("mytable") 
var rows = document.getElementById("noofrows").value; 
var tablerowcount = tableelement.rows.length; 
var currowcount = tablerowcount; 


for(i = 1; i < rows; i++) { 

var row = tableelement.insertRow(currowcount); 
var cellid = row.insertCell(0); 
cellid.innerHTML = "<b>" + currowcount +"</b>"; 
var cellid2 = row.insertCell(1); 


cellid2.innerHTML = "<input value='' id='" + currowcount +"_name' /> "; 
var cellid3 = row.insertCell(1); 
cellid3.innerHTML = "<input value='' id='" + currowcount +"_name' /> "; 
var cellid4 = row.insertCell(1); 
cellid4.innerHTML = "<input value='' id='" + currowcount +"_name' /> "; 
var cellid5 = row.insertCell(1); 
cellid5.innerHTML = "<input value='' id='" +currowcount +"_name' /> "; 
var cellid6 = row.insertCell(1); 
cellid6.innerHTML = "<input value='' id='" +currowcount+"_name' /> "; 


var currowcountcurrowcount = currowcount++; 


} 
} 
</script> 
</head> 
<body> 
<form method="post" action="insert.php> 

Carton Number :<input type="text" name="cn"> <br> 
Business Unit :<input type="text" name="bu"> <br> 
No of Files :<input type="noofrows" id="noofrows" value="1" name="nof" /> 

<input name="add" id="add" type="button" value="Add" onClick="AddRows()"/> 


<table name="mytable" id="mytable" border=1> 
    <tr> 
     <td> 
      <b>SN</b> 
     </td> 
     <td> 
      <b>From</b> 
     </td> 
     <td> 
      <b>To</b> 
     </td> 
     <td> 
      <b>Range</b> 
     </td> 
     <td> 
      <b>Document Category</b> 
     </td> 
     <td> 
      <b>Document Type</b> 
     </td> 
    </tr> 
    <tr> 
    <td> 
     <b>1</b> 
    </td> 
    <td> 
     <input value="" id='1_name' > 
    </b> 
</td> 
<td> 
     <input value="" id='1_name'/> 
    </b> 
</td> 
<td> 
    <input value="" id='1_name' /> 
</b> 
    </td> 

    <td> 
    <Select id='select' /> 
    <option name="option" value="finance">Finance Documents</option> 
    <option name="option" value="hr">HR Documents</option> 
    <option name="option" value="test">test</option 
    </Select> 
    </b> 
    </td> 
    <td> 

    <Select id='select' /> 
     <option name="option" value="CP">Cash movement</option> 
     <option name="option" value="PV">Payment Voucher</option> 
     <option name="option" value="RV">RV</option 
    </Select> 
    </b> 
    </td> 
</tr> 
</table> 
<br/> 
+0

只對你的HTML註釋:'id'屬性應該是唯一的。你有兩個'選擇'元素與「選擇」作爲'ID' – Joshua 2013-03-12 08:39:56

+0

這不是最終的代碼,我只是宣佈我的想法:) – user2156340 2013-03-12 09:42:57

回答

3
function AddRows() 
{ 
var tableelement = document.getElementById("mytable") 
var rows = document.getElementById("noofrows").value; 
var tablerowcount = tableelement.rows.length; 
var currowcount = tablerowcount; 


for(i = 1; i < rows; i++) { 

var row = tableelement.insertRow(currowcount); 
var cellid = row.insertCell(0); 
cellid.innerHTML = "<b>" + currowcount +"</b>"; 
var cellid2 = row.insertCell(1); 
cellid2.innerHTML = "<Select id='select' /><option name='option' value='CP'>Cash movement</option><option name='option' value='PV'>Payment Voucher</option> <option name='option' value='RV'>RV</option></Select> "; 
var cellid3 = row.insertCell(1); 
cellid3.innerHTML = "<Select id='select' /><option name='option' value='finance'>Finance Documents</option><option name='option' value='hr'>HR Documents</option><option name='option' value='test'>test</option</Select> "; 
var cellid4 = row.insertCell(1); 
cellid4.innerHTML = "<input value='' id='" + currowcount +"_name' /> "; 
var cellid5 = row.insertCell(1); 
cellid5.innerHTML = "<input value='' id='" +currowcount +"_name' /> "; 
var cellid6 = row.insertCell(1); 
cellid6.innerHTML = "<input value='' id='" +currowcount+"_name' /> "; 


var currowcountcurrowcount = currowcount++; 


} 
} 
</script> 

在你的函數變化得到的每一行 選擇並先保存數據更改的所有輸入和選擇id作爲數組變量
前範圍的id ='1_name_Range []'。也改變了功能 重命名代碼,以保存你所熟知的代碼。用數組變量保存在數據庫中

相關問題