dsvjw20866 2013-07-16 12:30
浏览 12

输出动态表数据

I am creating an invoice website for my 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.

 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. 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)

  • 写回答

1条回答 默认 最新

  • dts777777 2013-07-16 12:36
    关注

    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']. 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:

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

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接