PHP无法访问HTML表的行数据

时间:2019-01-25 07:38:56

标签: javascript php html html5 dom

我有一个HTML + PHP + JS组合页面,

enter image description here

HTML:

<form method="post">

<center>

<div id="divv">
    <TABLE id="dataTable" width="350px" border="1">

        <TR>
            <TD><INPUT type="checkbox" name="chk"/></TD>
            <TD style="width:80%"><?php echo $_GET['moduleno'];?></TD>
            <TD><SELECT name="mach">
                    <option value="">--Choose Machine--</option>
                    <OPTION value="F&K">F&K</OPTION>
                    <OPTION value="K&S">K&S</OPTION>
                </SELECT>    
            </TD>         
            <TD><INPUT type="text" placeholder="Enter Cell Cluster" name="txt"/></TD>
            <TD><INPUT type="text" placeholder="Enter Wire Number" name="txt2"/></TD>
           <TD><SELECT name="bond-position">
                    <option value="">--Choose Position--</option>
                    <OPTION value="Cell">Cell</OPTION>
                    <OPTION value="Busbar">Busbar</OPTION>
                    <OPTION value="VSB">VSB</OPTION>
                </SELECT>    
            </TD>
            <TD><SELECT name="typist">
                    <option value="">--Choose Type--</option>
                    <OPTION value="POS">POS</OPTION>
                    <OPTION value="NEG">NEG</OPTION>
                    <OPTION value="VSB">VSB</OPTION>
                </SELECT>    
            </TD>              
        </TR>
    </TABLE>
    <INPUT type="button" id="addbutton" value="Add Row" onclick="addRow('dataTable')" />

    <INPUT type="button" id="delbutton" value="Delete Row" onclick="deleteRow('dataTable')" />
<div>
</center>
<center><input type="submit" name="submit" id="sub1" value="Log Information & Proceed to Bond Resistance Check"  onclick="return confirm('Please press OK to Save/Log Information.')">&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="skip" id="sub2" value="Skip Step"  onclick="return confirm('Are you sure you want to skip this step? Everything will be marked as \'Step Skipped\'')"></center>
</form>
<a href="toc.php?moduleno=<?php echo $_GET['moduleno'];?>">Back to Table of Contents</a>&nbsp;<a href="index.php">Back to Home</a>

添加动态表行并将其删除的Javascript:

<SCRIPT language="javascript">
    function addRow(tableID) {

        var table = document.getElementById(tableID);

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

        var colCount = table.rows[0].cells.length;

        for(var i=0; i<colCount; i++) {

            var newcell = row.insertCell(i);

            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
            //alert(newcell.childNodes);
            switch(newcell.childNodes[0].name) {
                case "text":
                        newcell.childNodes[0].value = "<?php echo $_GET['moduleno'];?>"
                        break;
                case "checkbox":
                        newcell.childNodes[0].checked = false;
                        break;
                case "select-one":
                        newcell.childNodes[0].selectedIndex = 0;
                        break;
            }
        }
    }

    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) {
                if(rowCount <= 1) {
                    alert("Cannot delete all the rows!");
                    break;
                }
                table.deleteRow(i);
                rowCount--;
                i--;
            }


        }
        }catch(e) {
            alert(e);
        }
    }

PHP(将记录的值发送到数据库),

if (($_POST['submit'])&&($_SERVER["REQUEST_METHOD"] == "POST")) {

require "../config.php";
try {$connection = new PDO($dsn, $username, $password, $options);

$DOM = new DOMDocument();
$DOM->loadHTMLFile('wirebonding_1.php');
$rows = $DOM->getElementsByTagName("tr");
for ($i = 0; $i < $rows->length; $i++) {
    $cols = $rows->item($i)->getElementsbyTagName("td");
    for ($j = 0; $j < $cols->length; $j++) {        

        $new_user = array(
            "moduleno" => $_GET[$j->innertext],
            "machine"  => $_POST[$j->innertext],
            "cellcluster" => $_POST[$j->innertext],
            "wirenumber" => $_POST[$j->innertext],
            "position" => $_POST[$j->innertext],
            "type"     => $_POST[$j->innertext]


        );

        $sql = sprintf(
                "INSERT INTO %s (%s) values (%s)",
                "mydatabase.wirebonding_specifications",
                implode(", ", array_keys($new_user)),
                ":" . implode(", :", array_keys($new_user))
        );

        $statement = $connection->prepare($sql);
        $statement->execute($new_user);
        }}}
catch(PDOException $error) 
    {
        echo $sql . "<br>" . $error->getMessage();
    }

    exit;

}

我试图从HTML表中获取所有值,并将它们逐行插入到我的SQL表中。 我的PHP代码有误

我尝试在PHP代码中使用for循环,但似乎没有同时获取到这两行数据。如何修改代码,以便在按下“日志信息”按钮后将多行数据一一推送到SQL表?

非常感谢您的帮助!

0 个答案:

没有答案