bulrush Posted August 17, 2010 Share Posted August 17, 2010 I have multiple table rows on a screen which look like this: [pre] Group name | Copy items from Group Group1 | cbxCopyfrom [cmdCopyitems] Group2 | cbxCopyfrom [cmdCopyitems] Group3 | cbxCopyfrom [cmdCopyitems] [/pre] The table lists a group name, one per row. Each combo/select box lists all groups EXCEPT the one in the current row. Next to the combo/select box is a button cmdCopyitems. Sometimes we want to copy multiple items from Group2 to Group1. So in the row of Group1, in the combo box we choose Group2, then click the button next to it. When I submit the form like this: if (isset($_POST['cmdCopybullet']) ) { } How do I know which button was clicked? Which row was the button in? And how do I get the value in the cbxCopyfrom in the row where the button was clicked? Or am I going about this the wrong way? Quote Link to comment https://forums.phpfreaks.com/topic/210993-on-submit-which-table-row-am-i-in/ Share on other sites More sharing options...
ober Posted August 17, 2010 Share Posted August 17, 2010 You'll probably have to do this with Javascript. The onclick action in the button will pass a parameter to a JS function that then redirects to your page with a variable in the query string. Quote Link to comment https://forums.phpfreaks.com/topic/210993-on-submit-which-table-row-am-i-in/#findComment-1100471 Share on other sites More sharing options...
DavidAM Posted August 17, 2010 Share Posted August 17, 2010 Are you using INPUT type="submit" tags or are you using BUTTON tags? I have had more luck with the INPUT than with the BUTTON tags. I had a similar situation a while back. Since the value attribute is actually displayed, we can't really use it to indicate which button was pressed. But, since we can send an array to PHP using the name tag: name="cmdSubmit[]", we can use the row number as the index to the name: <INPUT type="submit" value="Copy Items" name="cmdCopy[1]"> <INPUT type="submit" value="Copy Items" name="cmdCopy[2]"> <INPUT type="submit" value="Copy Items" name="cmdCopy[3]"> if you click on the second button, you should get: $_POST['cmdCopy'][2] = 'Copy Items' Then we can check which button we received: if (isset($_POST['cmdCopy'])) { $group = array_keys($_POST['cmdCopy']); $group = $group[0]; We should only get one button (if you are using the INPUT type=submit) so that should tell us which one it is. Edit: I have not tested this thoroughly. But I think that was how I did it. Quote Link to comment https://forums.phpfreaks.com/topic/210993-on-submit-which-table-row-am-i-in/#findComment-1100512 Share on other sites More sharing options...
disciple10 Posted August 18, 2010 Share Posted August 18, 2010 I'm doing this in multiple places in my site right now, and in a couple different ways. I personally prefer using <button type='submit'> over <input type='submit'>. This way I can customize the display text and values separately. <button type='submit' name='copy' value='row_id'>Copy</button> Then all you need to do is check if(isset($_POST['copy'])) and copy the specific row which matches row_id. I would make this id have the same value as the name of the box you want to get the value of. Having multiple submit buttons is not a problem, just make sure the one you want to trigger on "Enter" is listed first. All of the buttons can have the same name, only 1 value will be passed. I am currently using this method successfully. Quote Link to comment https://forums.phpfreaks.com/topic/210993-on-submit-which-table-row-am-i-in/#findComment-1100602 Share on other sites More sharing options...
bulrush Posted August 18, 2010 Author Share Posted August 18, 2010 I am using <input type="submit">. I will look into your suggestions. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/210993-on-submit-which-table-row-am-i-in/#findComment-1100669 Share on other sites More sharing options...
DavidAM Posted August 18, 2010 Share Posted August 18, 2010 @disciple10 The problem I have had (and may others if you google it) is that IE (at least up to version 6, I have never tried with anything later) will submit ALL BUTTON tags regardless of which one you clicked. It also submits the button text (between the open and close tags) instead of the value (which FF and others use). I don't know what the standard says, but I have not found a way to use multiple BUTTON tags that is reliable across multiple browsers. <button type='submit' name='copy' value='1'>Copy</button> <button type='submit' name='copy' value='2'>Copy</button> <button type='submit' name='copy' value='3'>Copy</button> // If you click the second button, // In IE you get $_POST = array ('copy' => 'Copy'); // In FF you get $_POST = array ('copy' => '2'); It is easier to see what happens if we use different buttons: <button type='submit' name='cmdCopy' value='doCopy'>Copy</button> <button type='submit' name='cmdDelete' value='doDelete'>Delete</button> <button type='submit' name='cmdEdit' value='doEdit'>Edit</button> // If you click the second button, // In IE you get $_POST = array ('cmdCopy' => 'Copy', 'cmdDelete' => 'Delete', 'cmdEdit' => 'Edit'); // In FF you get $_POST = array ('cmdDelete' => 'doDelete'); There are many cases where I would prefer to use BUTTON over INPUT type=submit -- styling, non-string value attribute, etc. -- but IE just makes it impossible Quote Link to comment https://forums.phpfreaks.com/topic/210993-on-submit-which-table-row-am-i-in/#findComment-1100788 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.