4

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>
4
  • Your input field is not in any form. Commented Jun 6, 2011 at 18:00
  • The whole table was wrapped in a form, as I noted in my question. Commented Jun 6, 2011 at 18:02
  • You actually don't even need a form here. You could build a "delete link," (you could even make it a button with a Javascript "onclick" event) passing the record ID in a query parameter (e.g. /delete_expiration.php?id=1234). You could then handle the delete on that page. Commented Jun 6, 2011 at 18:11
  • This is a quick and dirty internal page to schedule FTP expirations. It doesn't need to be that fancy. Commented Jun 6, 2011 at 18:14

3 Answers 3

6

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
                }
            ?>
Sign up to request clarification or add additional context in comments.

5 Comments

Can a form exist inside another form? I didn't know that.
Actually, there is no need for an external form in this solution.
@afaolek There should not be a form wrapping the whole table, instead there need to be many small forms to handle the buttons. You could even have many forms on each line for "Edit", "Delete", "Change Order", etc. Often here the <input type="button" onClick="sometrick()" value="Click Me"> is used with javascript
@Wouter I agree with you. But in your statement that "there should not be a form wrapping the whole table...", is it wrong syntactically to nest a table in a form or it's just for the purpose of this question that this should be done? I like to know more. Thanks
@afaolek Basically it is wrong in this context. Sure you can use a table inside a form, but I just usually do it the other way around anyway. The point here is that for every button you need a separate form with a hidden id field. That way in the handling code you can check for a standard $_POST or $_GET variable. $_POST['id'] will change depending on the form the submit button is pressed in. Not sure how to explain this more clearly...
2

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

2 Comments

That's just too complicated for what I need.
Which is why I said you are better off with the solution Extrakun proposes, I still think using Javascript is a very viable solution for editing and also lower on server load usually, so it deserves mentioning in my eyes for this particular problem type. Especially if you expect many users, I would suggest this approach.
0

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

1 Comment

This is still a duplicate answer, but at least the code is complete.

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.