Jump to content

On submit, which table row am I in?


bulrush

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.