0

I have a script which generates a table containing numerous rows and coloumns. On each end of a row, is a Submit button, whose name is one of an array generated on running the script, and goes like: delete_CN[0], delete_CN[1] etc.

While processing the form POST, I need to determine which Submit button was clicked. Currently, when I see a print_r or dump_vars, I see the following: ["delete_CN"]=> array(1) { [0]=> string(6) "Delete" }, which does not give the array index of the button that was pressed.

How should I best go about doing this? I need to find the array index of the submit button which was pressed, or another way to uniquely identify the row of the Submit button, to process the data. The original form is dynamically generated by reading a zonefile, and the elements on the same row are all arrays.

4 Answers 4

1

the post data will always include all buttons but only the one which was clicked will have a value. this way you can determine easily which button was clicked by checking which value in the button array is not empty. (The value will be the text on the button as that text is defined in the value attribute).

Sign up to request clarification or add additional context in comments.

Comments

0

In HTML/PHP you could make each row in the table a seperate form, and add a

<input type="hidden" name="which_clicked" value="delete_CN[0]"> 

then look for $_POST['which_clicked']

or you could use jQuery and have a which value is inserted based on which submit button is clicked.

2 Comments

quite user-friendy button label :D
super user friendly, that button label would offer the user tea and biscuits
0

Be aware that a form can also be submitted using the ENTER key. In case this happens the browser handles it like the first button in the form was clicked.

Obviously, pressing ENTER when focus is on a submit-button sends the form using this currently focused button.

2 Comments

this is not correct, it depends on the focus, also you can catch the enter key and stop the event.
when focus is on a button ENTER submits the form using it, thats right. I adjusted my previous comment accordingly. When javascript is in the game, everything can happen, thats also right. My note above was targeted on a simple case and describes browser default-behaviour.
0

What I do, I create an array of input type hidden, with "unique value", and those value we assigned to each button. such as input type = "hidden" name = "txtID[]" id = "txtID[]" value = "15618" and for the button input type="submit" name="CityAdd15618" id="CityAdd15618" value="+"

then when submit, you just need to loop

$lTtl=count($_POST['txtID']); for($lCtr1=0; $lCtr1<$lTtl; $lCtr1++) { if (isset($_POST['CityAdd' . $_POST['txtID'][$lCtr1]])) break; } echo "ttl:" . $lCtr1 . "<br>\n"; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.