0

我想将这个动态创建的表行的所有记录保存到 mysql 数据库中,但我无法获取动态添加的字段的名称值,我可以在 $_POST 方法中使用它。请帮助我。还告诉我如何将 JavaScript var 转换为PHP 变量

<?php 
if(isset($_POST['buton_sumit']))
    {
$nav_id =1;
$item1=$_POST['name1'];
$item1=$_POST['name2'];

echo $item1 + $item2; 
    }
?>



<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE> Add/Remove dynamic rows in HTML table </TITLE>

</HEAD>
<BODY>

    <table id="extra" width="499" >
    <th width="24">R</th><th width="19">Sr.</th><th width="133">Item-Name</th><th width="144">Quantity</th><th width="145">price</th>
    </table>
    <form action="table1.php" method="post" name="input">
    <TABLE id="dataTable" width="350px" border="1">

        <TR>
            <TD width="24"><INPUT type="checkbox" name="chk"/></TD>
            <TD width="19"> 1 </TD>
            <TD width="133"> <INPUT type="text" name="name1"/> </TD>
            <TD width="133"> <INPUT type="text" name="qty1"/> </TD>
             <TD width="133"> <INPUT type="text" name="price1"/> </TD>
      </TR>
</TABLE>


    <INPUT type="button" value="Add Item" onclick="addRow('dataTable')" />

    <INPUT type="button" value="Remove Item" onclick="deleteRow('dataTable')" />
    <INPUT type="submit" value="End Sale" name="buton_sumit" />
    </form>
<SCRIPT language="javascript">
        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            cell1.appendChild(element1);

            var cell2 = row.insertCell(1);
            cell2.innerHTML = rowCount + 1;

            var cell3 = row.insertCell(2);
            var element2 = document.createElement("input");
            element2.type = "text";
            element2.name="name"+ cell2.innerHTML;
            var element2id="myid"+cell2.innerHTML;

            element2.setAttribute('id',element2id);

            cell3.appendChild(element2);

            var cell4 = row.insertCell(3);
            var element3 = document.createElement("input");
            element3.type = "text";
            element3.name="qty"+ cell2.innerHTML;

            cell4.appendChild(element3);

            var cell5 = row.insertCell(4);
            var element4 = document.createElement("input");
            element4.type = "text";
            element4.name="price"+ cell2.innerHTML;

            cell5.appendChild(element4);


        }

        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }
        }
javascript:(document.write(document.forms[0].elements[2].name))
var myname2=2;
    </SCRIPT>

</BODY>
</HTML>
4

2 回答 2

1

我使用了相同的代码,我的问题是,当我单击行号 2 的复选框时...在 Php 中,我总是得到结果,因为选中了行号 1 复选框,这是错误的...

enter code here
<html><body>
<?php

$col = $_POST['Colname'];
$dtype = $_POST['Datatype'];
$pk = $_POST['Pk'];
$dbname= $_POST['Dbname'];
$space=$_POST['Space'];
$concount=$_POST['Concount'];
$qps=$_POST['Qps']; 


echo "$dbname <br/> $space <br/> $concount <br/> $qps <br/> ";




 $result=count($col);
echo($result);


 while($result!=0)
 {
$result-=1;

echo "<table><tr>";
echo "<td>$col[$result]</td>";
echo "<td>$dtype[$result]</td>";
echo "<td>$pk[$result]</td>";
echo "</tr></table>";   

}
?>

</html></body>
于 2013-06-21T17:41:30.087 回答
0

你在 echo 上得到的错误/输出是什么?

还使用隐藏字段将行数“rowCount”发送到服务器。此行数将帮助您定义循环的终点

添加到 HTML

 <input type="hidden" id="rowCount"/>

在 JS 代码中

<script>
var objInputFieldRowCount = docuement.getElemendbyId("rowCount");
var jsRowCount = table.getElementsByTagName("TR").length;
objInputFieldRowCount.value = jsRowCount;
</script>
于 2012-07-14T09:29:27.943 回答