【问题标题】:How to Display Multiple Rows of a Dynamic HTML Table in a Confirmation Page of Form如何在表单的确认页面中显示多行动态 HTML 表格
【发布时间】:2016-09-09 05:14:08
【问题描述】:

我正在创建一个 HTML 表单,其中包含一个可以动态添加行的表格。每当在多行中输入数据并点击提交时,它会将我带到确认页面,其中仅显示表中的一行数据。如何显示多行?

HTML(原始格式):

<p>
<div id="rebate_400p">
<strong>400P</strong><br>
</div>

<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" name="purchase_minimum"></td>
        <td><input type="text" id="rebate_tables" name="multiplier"></td>
        <td><input type="text" id="rebate_tables" name="uom"></td>
        <td><input type="text" id="rebate_tables" name="retro"></td>
        <td><input type="text" id="rebate_tables" name="guaranteed"></td>
        <td><input type="text" id="rebate_tables" name="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>
</p>

HTML/PHP(确认页面):

<p>
<div class="border1">
<div id="rebate_400p">
<strong>400P</strong><br>
</div>

<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>
    <tr align="center">
        <td>1</td>
        <td><?php echo $_POST['purchase_minimum']; ?></td>
        <td><?php echo $_POST['multiplier']; ?></td>
        <td><?php echo $_POST['uom']; ?></td>
        <td><?php echo $_POST['retro']; ?></td>
        <td><?php echo $_POST['guaranteed']; ?></td>
        <td><?php echo $_POST['paid']; ?></td>
    </tr>
</table>
</div>
</p>

Javascript(在原始表单页面上动态添加行):

function insRow()
{

  console.log('hi');
  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='';


  x.appendChild(new_row)
}

【问题讨论】:

  • 你的 PHP 只是你的 HTML...
  • 是的,它主要是 HTML,只有几行 php 代码。我点击原始表单上的提交按钮后,它是“确认页面”。
  • 附加输入的代码是什么? JS?
  • 我刚刚用 javascript 更新了我的帖子...这是我拥有的唯一允许表格动态添加行的 JS。
  • 好吧.. 实际上没有表单.. 你是把表单从 sn-p 中去掉还是通过 ajax 提交?

标签: php html forms html-table


【解决方案1】:

好的。因此,您的问题是您的输入具有相同的名称,因此当它们在 $_POST 中传递时,它们会相互覆盖。此外,即使不是这种情况,也无法避免确认页面上出现 PHP 循环。

在你的 JS 函数中:

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

在这里,当我们追加新行时,我们为输入赋予了一个新名称,因此它们在传递时不会被覆盖。

在您的 HTML 表单中:

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

我们正在将您的输入更改为数组,以便可以传递多个值。

在您的确认页面上:

<?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; ?>

我们只是遍历每一行。

【讨论】:

  • 耶!很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2016-12-21
  • 2012-03-29
  • 2014-06-14
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多