简体   繁体   English

显示动态 HTML 表中的数据

[英]Displaying Data From Dynamic HTML Table

I have an HTML form that you can input values into.我有一个 HTML 表单,您可以在其中输入值。 You can click submit then it will show the data on a confirmation page that you entered.您可以单击提交,然后它会在您输入的确认页面上显示数据。 I was having problems with displaying multiple lines of text but got that fixed.我在显示多行文本时遇到问题,但已解决。 Now I have another problem...I have 3 sets of tables and whenever information is entered (specifically in the 3rd table), it copies that data into the other table...how can this be fixed??现在我有另一个问题……我有 3 组表,每当输入信息(特别是在第三个表中)时,它都会将该数据复制到另一个表中……如何解决?

This is the HTML for the table and looks the same for each of the 3 tables:这是表格的 HTML,并且对于 3 个表格中的每一个看起来都相同:

<table id="tables" cellspacing="5">
    <tr align="center" id="table_titles">
        <td>Tier</td>
        <td>Purchase Minimum</td>
        <td>Multiplier</td>
        <td>UOM</td>
        <td>Retro</td>
        <td>Guaranteed</td>
        <td>Paid</td>
        <td>Delete?</td>
        <td>Add Row</td>
    </tr>
    <tr>
            <td align="center" name="tier">1</td>
            <td><input type="text" id="rebate_tables" data-name="purchase_minimum" name="rows[0][purchase_minimum]"></td>
            <td><input type="text" id="rebate_tables" data-name="multiplier" name="rows[0][multiplier]"></td>
            <td><input type="text" id="rebate_tables" data-name="uom" name="rows[0][uom]"></td>
            <td><input type="text" id="rebate_tables" data-name="retro"  name="rows[0][retro]"></td>
            <td><input type="text" id="rebate_tables" data-name="guaranteed" name="rows[0][guaranteed]"></td>
            <td><input type="text" id="rebate_tables" data-name="paid" name="rows[0][paid]"></td>
            <td><input type="button" id="delRow" value="Delete" onclick="deleteRow(this)"></td>
            <td><input type="button" id="addmoreRowsbutton" value="Add row" onclick="insRow()"></td>
        </tr>
</table>

This is the JAVASCRIPT for each table and is extremely similar to that of the other tables except for the insRow() has a different name as well as the new_row :这是每个表的 JAVASCRIPT 并且与其他表的非常相似,除了insRow()具有不同的名称以及new_row

function insRow()
{
  var x=document.getElementById('tables');
  var new_row = x.rows[1].cloneNode(true);
  var len = x.rows.length;
  new_row.cells[0].innerHTML = len;

  var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
  inp1.id += len;
  inp1.value = '';
  var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
  inp2.id += len;
  inp2.value = '';

  var inputs = new_row.querySelectorAll('input[type=text]');

  for(var i=0;i<inputs.length;i++)
  {
      inputs[i].value='';
      inputs[i].name='rows[' + len + '][' + inputs[i].dataset.name + ']';
  }

  x.appendChild(new_row)
}

And this is the HTML/PHP confirmation page with each table having the same code:这是每个表具有相同代码的 HTML/PHP 确认页面:

<?php if(isset($_POST['rows'])): ?>
    <table id="table" cellspacing="20">
        <tr align="center" id="table_row">
            <td>Tier</td>
            <td>Purchase Minimum</td>
            <td>Multiplier</td>
            <td>UOM</td>
            <td>Retro</td>
            <td>Guaranteed</td>
            <td>Paid</td>
        </tr>
        <?php 
            $count = 1;
            foreach($_POST['rows'] as $row): 
        ?>
            <tr align="center">
                <td><?php echo $count; ?></td>
                <td><?php echo $row['purchase_minimum']; ?></td>
                <td><?php echo $row['multiplier']; ?></td>
                <td><?php echo $row['uom']; ?></td>
                <td><?php echo $row['retro']; ?></td>
                <td><?php echo $row['guaranteed']; ?></td>
                <td><?php echo $row['paid']; ?></td>
            </tr>
        <?php 
            $count++;
            endforeach; 
        ?>
    </table>
<?php endif; ?>

There is a big problem in your html code: several elements have the same ids.您的 html 代码中存在一个大问题:多个元素具有相同的 id。 Ids must be unique. ID 必须是唯一的。 Each table must have a different id than the other tables.每个表必须具有与其他表不同的 id。 Each input must have a different id than the others.每个输入必须具有与其他输入不同的 ID。

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

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