Php and html for distinguishing between multiple submit buttons of same name?

i’m trying to do a table, each row containing some data followed by its own ‘edit’ and ‘delete’ buttons. so there’s an edit button and delete button on each row. this is what the outputted html, which is contained in a post form, currently looks like:

	<td><input type="submit" name="edit-1" value="Edit" /></td>
	<td><input type="submit" name="delete-1" value="Delete" /></td>
	<td><input type="submit" name="edit-2" value="Edit" /></td>
	<td><input type="submit" name="delete-2" value="Delete" /></td>

i thought i’d be able to then extract the name value but that doesn’t seem to be passed on to the receiving script. is that correct? i get the value value; Edit or Delete. i suppose i could put the number in the button but i’d prefer not to. anyway of passing on which of the multiple buttons was clicked without putting the value visibly on the button? i suppose i could have multiple forms, one for each row, which would also do it but i’m wondering if there’s a simpler way?


<input type=‘submit’ name=‘Submit_1’ value=‘Edit’ />
<input type=‘submit’ name=‘Submit_2’ value=‘Delete’ />


i see, i think. so if i had many submit buttons in the same way as the html you’ve posted, the code to find out which submit button was clicked might be something like:

$numberofpossiblebuttons = ...

$i = 0;
while( $i < $numberofpossiblebuttons && empty($submitbutton) ) {
	if( isset($_POST['Submit_' . $i]) ) $submitbutton = $i;

is that the/a way to do it? i’ll try it.


excellent yes that basically worked. had to contruct the name string before putting it the $POST array index: $name = 'Submit’ . $i…


It might be easier to read the code to do this unless you have bunch of submit buttons

if (isset($_POST['delete'])) {
    // delete whatever
} else if (isset($_POST['edit'])) {
    // edit whatever

<input type='submit' name='delete' value='Delete' />
<input type='submit' name='edit' value='Edit' />

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.

    <td><input type="submit" name="submit[1]" value="Edit" /></td>
    <td><input type="submit" name="submit[1]" value="Delete" /></td>
    <td><input type="submit" name="submit[2]" value="Edit" /></td>
    <td><input type="submit" name="submit[2]" value="Delete" /></td>

Now you can determine which button was clicked fairly easily since PHP will want to treat it as an array with only one element.

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:

		<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
				Row 1
				<input type="submit" name="submit[1]" value="Edit" />
				<input type="submit" name="submit[1]" value="Delete" />
				Row 2
				<input type="submit" name="submit[2]" value="Edit" />
				<input type="submit" name="submit[2]" value="Delete" />

		<?php if (isset($_REQUEST['submit'])): ?>

			<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>

		<?php endif; ?>


BTW - This style of element naming does not work if you use quotes for the array keys in your HTML:

<!-- This does not work properly -->
<input type="submit" name="submit['2']" value="Edit" />

not only do i have a bunch of submit buttons but also i have a variable number so it can’t be hard coded like that. thanks.

yes i thought there must be something like that which allows you to refer to it directly once somehow. i was quite surprised i couldn’t ask for it by the html attribute’s name, “name” in this case but it doesn’t look like that doesn’t get transmitted with the form submission.

great, thanks :slight_smile:

works a treat. i was surprised that the square brackets in the name attribute validated though. i just wrote a function which turns a string into an appropriate string for use in name attributes and when doing so referred to which says

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (“-”), underscores (“_”), colons (“:”), and periods (“.”).

no square bracket characters mentioned. but it does validate so that’s that.


I’m glad you looked that up, I never even thought of it. Good to know.

i made a mistake: the ID and NAME bit i quoted from the html w3 specs is not at all related to the name attribute. the character data type that is applicable to name attributes in the html specs is CDATA, and “CDATA is a sequence of characters from the document character set and may include character entities.” so that obviously includes square brackets.