我创建了带有动态表的简单表单。该表行根据用户输入值增加,因此每次创建新行时,我都需要增加组合框或选择(选项),因此它应该类似于默认组合框值。注意:此选择框或组合框是列属性的一部分,请记住行递增,但不包含列。

第二个问题:

我想收集此表的列数据或值(无论其行号是多少),并使用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/>

最佳答案

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更改为数组变量
ex id ='1_name_Range []'表示范围。功能也会改变
如您所说,重命名代码以进行保存。使用数组变量保存在数据库中

07-24 16:44
查看更多