如何通过单击按钮添加更多行来更新表

时间:2015-10-19 16:47:01

标签: php button html-table

我尝试使用在文本框中输入的用户请求更新表格。 因此,如果用户希望有5行输入订单中的数据,他们会输入' 5'在文本框中,点击更新按钮,它将填充表5行。 我相信我的代码中存在逻辑,但由于某种原因它不会更新。有什么想法吗?

<table>
<tr>
    <th class="textCol">Product Rows:</th>
    <td class="inputCol"><input type="text" name="rows"></td>
    <td><input class="update" type="button" name="update" value="Update"></td>
    <td class="inputCol"></td>
</tr>
</table>

<table class="bottomTable">
    <tr>
        <th class="textCol">Product</th>
        <th class="textCol">Quantity</th>
        <th class="textCol">Unit Price</th>
        <th class="textCol">Total Price</th>
    </tr>
<?
    if (isset($_POST['update']))
    {
        //Execute this code if the update button is clicked.
        $num = $_POST['rows'];

        for ($i=0; $i<$num; $i++) { ?>
          echo "<tr>
                    <td class="inputCol2"><input type="text" name="product[$i]" ></td>
                    <td class="inputCol2"><input type="text" name="quantity[$i]" ></td>
                    <td class="inputCol2">$<input type="text" name="unit[$i]" ></td>
                    <td class="inputCol2">$<input type="text" name="total[$i]" ></td>
                </tr>";
    <? } ?>
<? } else {?>

    <tr>
        <td class="textCol"></td>
        <td class="textCol"></td>
        <td class="textCol">Total Order:</td>
        <td class="inputCol2">$<input type="text" name="total6"></td>
    </tr>
<? } ?>

</table>

2 个答案:

答案 0 :(得分:2)

输入是否在表单中?如果它不是一个表单,它默认提交为$ _GET,而不是$ _POST,所以if()总是会发现它为FALSE。

答案 1 :(得分:0)

<form method="post">
<table>
    <tr>
        <th class="textCol">Product Rows:</th>
        <td class="inputCol"><input type="text" name="rows"></td>
        <td><input class="update" type="submit" name="update" value="Update"></td>
        <td class="inputCol"></td>
    </tr>
    <tr>
        <th class="textCol">Product</th>
        <th class="textCol">Quantity</th>
        <th class="textCol">Unit Price</th>
        <th class="textCol">Total Price</th>
    </tr>
<?
    if (isset($_POST['update']))
    {
        //Execute this code if the update button is clicked.
        $num = $_POST['rows'];

        for ($i=0; $i<$num; $i++) { ?>
            <tr>
                <td class="inputCol2"><input type="text" name="'product' . <?=[$i]?>" ></td>
                <td class="inputCol2"><input type="text" name="'quantity' . <?=[$i]?>" ></td>
                <td class="inputCol2">$<input type="text" name="'unit' . <?=[$i]?>" ></td>
                <td class="inputCol2">$<input type="text" name="'total' . <?=[$i]?>" ></td>
            </tr>
    <? } ?>
<? } else {?>
    <tr>
        <td class="textCol"></td>
        <td class="textCol"></td>
        <td class="textCol">Total Order:</td>
        <td class="inputCol2">$<input type="text" name="total6"></td>
    </tr>
<? } ?>
</table>
</form>

我的价值观的php仍然不正确,但我目前正在努力。但它现在可以正确更新行。谢谢你的帮助!