简体   繁体   English

输出动态表数据

[英]Output Dynamic Table Data

I am creating an invoice website for my intranet. 我正在为我的Intranet创建发票网站。

I have the following code for making a dynamic table row inside the html that generates rows up to how much we need for the invoice. 我有以下代码,用于在html内制作动态表格行,该行生成的行数最多等于我们需要发票的数量。

 function addRow(tableID) {

        var table = document.getElementById(tableID);

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

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

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

            var newcell = row.insertCell(i);

            newcell.innerHTML = table.rows[1].cells[i].innerHTML;
            //alert(newcell.childNodes);
            switch(newcell.childNodes[0].type) {
                case "text":
                        newcell.childNodes[0].value = "";
                        break;
                case "checkbox":
                        newcell.childNodes[0].checked = false;
                        break;
                case "select-one":
                        newcell.childNodes[0].selectedIndex = 0;
                        break;
            }
        }
    }

After inserting data through the form, I want to output the table data through php echo. 通过表单插入数据后,我想通过php echo输出表数据。 But I can't figure out how to output every row from the dynamic table because right now it just gives me the last row. 但是我不知道如何从动态表中输出每一行,因为现在它只给我最后一行。

In the following fashion I want to output the data: 我要以以下方式输出数据:

echo $var_name."<br>";

Right now I have 5 variables for each row in the table, but they only give me the latest row input (logical) 现在,表中的每一行都有5个变量,但它们只给我最新的行输入(逻辑)

You need to give the form fields names ending in [] . 您需要给表单字段名称以[]结尾。 When the form is submitted, PHP will combine all the fields with the name name[] into an array in $_POST['name'] . 提交表单后,PHP会将名称为name[]所有字段组合到$_POST['name']的数组中。 You can then iterate through these arrays to get all the rows of the form. 然后,您可以遍历这些数组以获取表单的所有行。

The form should look something like: 该表格应类似于:

<form method="post" action="your URL">
  <table>
    <tr><td><input type="text" name="somename[]"></td>
        <td><select name="somemenu[]">
              <option value="">Please choose</option>
              <option value="1">Option 1</option>
              <option value="2">Option 2</option>
              ...
            </select></td>
    </tr>
  </table>
</form>

Then your PHP can do: 然后您的PHP可以执行以下操作:

foreach (array_keys($_POST['somename']) as $key) {
  // Do something with $_POST['somename'][$key] and $_POST['somemenu'][$key]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM