输出动态表格数据 [英] Output Dynamic Table Data

查看:111
本文介绍了输出动态表格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下代码用于在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; (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){
casetext:
newcell.childNodes [0] .value =;
休息;
case复选框:
newcell.childNodes [0] .checked = false;
休息;
caseselect-one:
newcell.childNodes [0] .selectedIndex = 0;
休息;



code
$ b

通过表单插入数据后,我想通过php回显输出表格数据。但我无法弄清楚如何输出动态表中的每一行,因为现在它只是给了我最后一行。



以下列方式我想输出数据:

  echo $ var_name。< br>; 

现在我在表格中的每一行都有5个变量,但他们只给了我最新的一行input(logical)

解决方案

您需要提供以 [] 。当提交表单时,PHP将把 name [] 的所有字段合并到 $ _ POST ['name'] 。您可以遍历这些数组以获取表单的所有行。



表单应该类似于:

 < form method =postaction =your URL> 
< table>
< tr>< td>< input type =textname =somename []>< / td>
< td>< select name =somemenu []>
< option value =>请选择< / option>
< option value =1>选项1< / option>
< option value =2>选项2< / option>
...
< / select>< / td>
< / tr>
< / table>
< / form>

然后你的PHP可以这样做:

<$ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' 'somemenu'] [$ key]
}


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)

解决方案

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]
}

这篇关于输出动态表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆