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>
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
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
精彩评论