Nik
Nik

Reputation: 7273

PHP Dynamic HTML Question

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>

Upvotes: 4

Views: 746

Answers (3)

Chris Laarman
Chris Laarman

Reputation: 1589

You should set a hidden field containing the id somewhere in your row, and wrap the hidden field and the submit button in a form per row:

<form>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="submit" value="Delete" />
</form>

After clicking, get the id with $_POST['id'].

Greetz,

XpertEase

Upvotes: 0

Wouter Simons
Wouter Simons

Reputation: 2906

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

Upvotes: 2

Extrakun
Extrakun

Reputation: 19305

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

Upvotes: 6

Related Questions