0

I'm trying to update a table with the user's request entered in a text box. So if a user wanted to have 5 rows to enter in data in an order form, they would type in '5' in the text box, hit the update button and it would populate the table with 5 rows. I believe I have the logic down in my code, but for some reason it isn't updating. Any ideas why?

<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>
1
  • where is the code to update?? Commented Oct 19, 2015 at 16:49

2 Answers 2

2

Is the input in a form? If it isn't in a form, it submits as a $_GET by default, not a $_POST, so the if() would always find it to be FALSE.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I'd forgotten that it defaults to $_GET method. I added form tags around the table and also changed the input of the update button to 'submit' since it was just 'button' and it works!
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>

The php for my values are still not correct, but I'm working on that currently. But it now updates the rows correctly. Thank you for your help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.