I have this piece of PHP/HTML, wrapped in a POST form. How do I pass only the ID for the row where the delete button is clicked back to the server?
        <table>
            <tr>
                <th>File Path</th>
                <th>Expiration</th>
            </tr>
            <?php
                $result = mysql_query ($list_query);
                $row_count = mysql_numrows ($result);
                for ($i = 0; $i < $row_count; $i++) {
                    $id = mysql_result ($result, $i, "id");
                    $path = mysql_result ($result, $i, "path");
                    $expiration = mysql_result ($result, $i, "expires");
            ?>
                    <tr>
                        <td width="60%">
                            <?php echo $path; ?>
                        </td>
                        <td>
                            <?php echo $expiration; ?>
                        </td>
                        <td>
                            <input type="submit" value="Delete Expiration" />
                        </td>
                    </tr>
            <?php
                }
            ?>
        </table>
                Use a hidden field within the form.
<input type="hidden" name="id" value="<?php echo $id ?>">
You should also start a new form for each new row too.
 for ($i = 0; $i < $row_count; $i++) {
                    $id = mysql_result ($result, $i, "id");
                    $path = mysql_result ($result, $i, "path");
                    $expiration = mysql_result ($result, $i, "expires");
            ?>
                    <tr>
                        <td width="60%">
                            <?php echo $path; ?>
                        </td>
                        <td>
                            <?php echo $expiration; ?>
                        </td>
                        <td>
                         <form method="POST" action="?">
                           <input type="hidden" name="id" value="<?php echo $id ?>">
                           <input type="submit" value="Delete Expiration" />
                         </form>
                        </td>
                    </tr>
            <?php
                }
            ?>
                        I agree with the solution Extrakun proposes, however, for completeness I would like to point you to the option of using JavaScript and DOM. You could use JQuery as explained in this question:
jquery + table row edit - String problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With