You can also name your submit buttons in an array format. This will enable you to get your values without creating extra loops in your PHP.
Code:
<tr>
<td>Some</td>
<td>data</td>
<td>here</td>
<td><input type="submit" name="submit[1]" value="Edit" /></td>
<td><input type="submit" name="submit[1]" value="Delete" /></td>
</tr>
<tr>
<td>Some</td>
<td>data</td>
<td>here</td>
<td><input type="submit" name="submit[2]" value="Edit" /></td>
<td><input type="submit" name="submit[2]" value="Delete" /></td>
</tr>
Now you can determine which button was clicked fairly easily since PHP will want to treat it as an array with only one element.
PHP Code:
if (isset($_REQUEST['submit'])) {
/**
* $_REQUEST['submit'] is an array with only one element.
* We can get the key for this "array" to find the number of the
* submit element.
*/
$submitNumber = array_pop(array_keys($_REQUEST['submit']));
// Now the value, "Edit" or "Delete"
$submitValue = array_pop($_REQUEST['submit']);
}
An example php file:
PHP Code:
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p>
Row 1
<input type="submit" name="submit[1]" value="Edit" />
<input type="submit" name="submit[1]" value="Delete" />
</p>
<p>
Row 2
<input type="submit" name="submit[2]" value="Edit" />
<input type="submit" name="submit[2]" value="Delete" />
</p>
</form>
<?php if (isset($_REQUEST['submit'])): ?>
<dl>
<dt>Submitted row number:</dt>
<dd><?php echo array_pop(array_keys($_REQUEST['submit'])) ?></dd>
<dt>Submitted value:</dt>
<dd><?php echo array_pop($_REQUEST['submit']) ?></dd>
</dl>
<?php endif; ?>
</body>
</html>
BTW - This style of element naming does not work if you use quotes for the array keys in your HTML:
Code:
<!-- This does not work properly -->
<input type="submit" name="submit['2']" value="Edit" />
Bookmarks