【问题标题】:Passing Form Data to separate PHP Page将表单数据传递给单独的 PHP 页面
【发布时间】:2017-10-14 11:11:23
【问题描述】:

我有一个包含多列的表 (index.php)。一列是一个复选框。 Whenever the checkbox is checked, it displays another row where you can select a quantity.然后,您可以点击一个名为“添加到订单”的按钮,它将带您进入确认页面 (index-order.php),我希望它显示每一行以及具有复选框的指定行中的所有数据检查。目前,我的控制台没有出现任何错误,但根本没有显示任何数据。

我需要进行哪些更改才能做到这一点?这是我目前所拥有的。

Index.php 代码:

<form name="form1" method="POST" action="index-order.php"> 

<section id="addToOrder">   
<button type="submit" class="order" id="order" name="order" value="AddToOrder">Add to Order</button>
</section>

<br>


<div id="my-div2" class="ui-widget">
<div class="ui-widget">

<table id="merchTable" cellspacing="5" class="sortable">
    <thead>
        <tr class="ui-widget-header">
            <th class="sorttable_nosort"></th>
            <th class="sorttable_nosort">Loc</th>
            <th class="merchRow">Report Code</th>
            <th class="merchRow">SKU</th>
            <th class="merchRow">Special ID</th>
            <th class="merchRow">Description</th>
            <th class="merchRow">Quantity</th>
            <th class="sorttable_nosort">Unit</th>
            <th style="display: none;" class="num">Quantity #</th>
        </tr>
    </thead>
    <tbody>

        <?php foreach ($dbh->query($query) as $row) {?>

        <tr>
        <td class="ui-widget-content"><input type="checkbox" class="check" name="check"></td>
        <td name="rows[0][0][loc]" class="loc ui-widget-content" id="loc-<?php echo intval ($row['Loc'])?>"><?php echo $row['Loc'];?></td>
        <td name="rows[0][0][rp-code]" class="rp-code ui-widget-content" align="center" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
        <td name="rows[0][0][sku]" class="sku ui-widget-content" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
        <td name="rows[0][0][special-id]" class="special-id ui-widget-content" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
        <td name="rows[0][0][description]" class="description ui-widget-content" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
        <td name="rows[0][0][quantity]" class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
        <td name="rows[0][0][unit]" class="unit ui-widget-content" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
        <td name="rows[0][0][quant]" style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" name="value" id="test"></td>
    </tr>

    <?php } ?>

    </tbody>
</table>
</div>

</div>

</form>

索引顺序.php:

<?php if(isset($_POST['rows'])): ?>
<table cellspacing="20">
    <tr align="center">
        <th>Loc</th>
        <th>Report Code</th>
        <th>SKU</th>
        <th>Special ID</th>
        <th>Description</th>
        <th>Quantity</th>
        <th>Unit</th>
        <th>Quantity #</th>
    </tr>
    <?php 
        foreach($_POST['rows'][0] as $row): 
    ?>
        <tr align="center">
            <td><?php echo $row['loc']; ?></td>
            <td><?php echo $row['rp-code']; ?></td>
            <td><?php echo $row['sku']; ?></td>
            <td><?php echo $row['special-id']; ?></td>
            <td><?php echo $row['description']; ?></td>
            <td><?php echo $row['quantity']; ?></td>
            <td><?php echo $row['unit']; ?></td>
            <td><?php echo $row['quant']; ?></td>
        </tr>
    <?php 
        endforeach; 
    ?>
</table>

【问题讨论】:

  • 我怀疑您是否能够针对带有名称属性的&lt;td&gt; 使用 POST 数组。也许用 JS,但肯定不是纯 php。
  • 如果您不想使用 JS,可以使用表中的隐藏输入来传递值。
  • “我的控制台没有错误” - 也许没有;尝试错误报告php.net/manual/en/function.error-reporting.php --- 然而,your previous question 建议您正在使用 JS,但没有在您的问题中包含它。因此这个问题不清楚。

标签: php forms post html-table


【解决方案1】:

我对前面的答案没意见,我从未听说过这种 PHP 方法,而且它似乎不是正确的解决方案。无论如何,以下帖子可能会对您有所帮助:How to get value from td's via $_POST.

您不能使用 td 通过 POST 传输数据;但另一种选择是使用“隐藏”类型的表单元素:

<form action="script.php" method="post"> <td class=".."><input type="hidden" name="td1" value="...">value</td> ... </form>

在 PHP 中,您将使用 $_POST 数组和 td1 名称获取数据:

<?php var_dump($_POST); ?>

在我看来,它会是更容易以适当的方式得到你想要的东西的方法;我给上面的链接也是在谈论 DOMDocument,但它看起来管理起来更复杂。

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 我说这篇文章可能会有所帮助,你是对的,我应该说的是基本部分。已更新。
猜你喜欢
  • 2011-07-03
  • 2014-05-12
  • 2013-04-13
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 2013-05-13
相关资源
最近更新 更多