Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Dynamic HTML Question

Tags:

php

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>
like image 347
Nik Avatar asked Feb 24 '23 23:02

Nik


2 Answers

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
                }
            ?>
like image 129
Extrakun Avatar answered Mar 07 '23 05:03

Extrakun


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

like image 26
Wouter Simons Avatar answered Mar 07 '23 05:03

Wouter Simons