PHP dynamic HTML question

I have this part of PHP / HTML wrapped in a POST form. How to transfer only the identifier for the line on which the delete button is pressed 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
source share
3 answers

Use the hidden field in the form.

 <input type="hidden" name="id" value="<?php echo $id ?>"> 

You must also start a new form for each new line.

  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 } ?> 
+6
source

I agree with the solution that Extrakun offers, however, for completeness, I would like to point out to you the possibility of using JavaScript and the DOM. You can use jQuery as described in this question:

jquery + table row editing - row problem

+2
source

You must set the hidden field containing the identifier somewhere on your line, and wrap the hidden field and submit button in the form for each line:

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

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

Greetz,

XpertEase

0
source

All Articles