Form with multiple submit buttons

View: New views
5 Messages — Rating Filter:   Alert me  

Form with multiple submit buttons

by nitin gupta-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I am having two form submit buttons on a single page, somethings like this:

$form['submit1'] = array(
    '#type' => 'submit',
    '#value' => t('save'),
    '#submit' => array('test_submit1'),
    
  );
  
[some more for elements in between]

  $form['submit2'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#submit' => array('test_submit2'),
    
  );


Now, whether I click on any of them, the submit callback of second button is called always. But if I change the value of second button or give them different '#name', everything gets back in place. They call their submit functions respectively.

I did not expect such a behavior given this:


Although, they both have same #value but will have different ids in the form, so Drupal has every reason to differentiate between them (or not??). Why is such thing happening? Am I missing something?

--
Regards,
Nitin Kumar Gupta
http://publicmind.in/blog/

Re: Form with multiple submit buttons

by Ken Winters :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If you look at the HTML source, the submit button's name is "op" - in the comment form, this lets you switch whether to preview or post, for example.  The comment module is probably a good place to look at code, too.

- Ken Winters

On Oct 6, 2009, at 3:57 PM, nitin gupta wrote:

Hello,

I am having two form submit buttons on a single page, somethings like this:

$form['submit1'] = array(
    '#type' => 'submit',
    '#value' => t('save'),
    '#submit' => array('test_submit1'),
    
  );
  
[some more for elements in between]

  $form['submit2'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#submit' => array('test_submit2'),
    
  );


Now, whether I click on any of them, the submit callback of second button is called always. But if I change the value of second button or give them different '#name', everything gets back in place. They call their submit functions respectively.

I did not expect such a behavior given this:


Although, they both have same #value but will have different ids in the form, so Drupal has every reason to differentiate between them (or not??). Why is such thing happening? Am I missing something?

--
Regards,
Nitin Kumar Gupta
http://publicmind.in/blog/


Re: Form with multiple submit buttons

by Steven Jones-9 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

When the browser posts the form back to the web server it only sends
the value of the form elements against their respective names. That
is, the following input

<input type="sometype" value="value" id="some-id" name="some-name" />

would be posted back to the server as
some-name = value.

Since buttons are also posted back in the same way there is not way
for Drupal know which button was pressed if they share the same name
and value.

Regards
Steven Jones
ComputerMinds ltd - Perfect Drupal Websites

Phone : 024 7666 7277
Mobile : 07702 131 576
Twitter : darthsteven
http://www.computerminds.co.uk



2009/10/6 nitin gupta <nitingupta.iitg@...>:

> Hello,
> I am having two form submit buttons on a single page, somethings like this:
> $form['submit1'] = array(
>     '#type' => 'submit',
>     '#value' => t('save'),
>     '#submit' => array('test_submit1'),
>
>   );
>
> [some more for elements in between]
>   $form['submit2'] = array(
>     '#type' => 'submit',
>     '#value' => t('Save'),
>     '#submit' => array('test_submit2'),
>
>   );
>
> Now, whether I click on any of them, the submit callback of second button is
> called always. But if I change the value of second button or give them
> different '#name', everything gets back in place. They call their submit
> functions respectively.
> I did not expect such a behavior given this:
> http://drupal.org/node/144132#buttons
> Although, they both have same #value but will have different ids in the
> form, so Drupal has every reason to differentiate between them (or not??).
> Why is such thing happening? Am I missing something?
> --
> Regards,
> Nitin Kumar Gupta
> http://publicmind.in/blog/
>

Re: Form with multiple submit buttons

by nitin gupta-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

oh , thats the catch.. thanks steven
--
Regards,
Nitin Kumar Gupta
http://publicmind.in/blog/


On Wed, Oct 7, 2009 at 2:36 AM, Steven Jones <steven.jones@...> wrote:
Hi,

When the browser posts the form back to the web server it only sends
the value of the form elements against their respective names. That
is, the following input

<input type="sometype" value="value" id="some-id" name="some-name" />

would be posted back to the server as
some-name = value.

Since buttons are also posted back in the same way there is not way
for Drupal know which button was pressed if they share the same name
and value.

Regards
Steven Jones
ComputerMinds ltd - Perfect Drupal Websites

Phone : 024 7666 7277
Mobile : 07702 131 576
Twitter : darthsteven
http://www.computerminds.co.uk



2009/10/6 nitin gupta <nitingupta.iitg@...>:
> Hello,
> I am having two form submit buttons on a single page, somethings like this:
> $form['submit1'] = array(
>     '#type' => 'submit',
>     '#value' => t('save'),
>     '#submit' => array('test_submit1'),
>
>   );
>
> [some more for elements in between]
>   $form['submit2'] = array(
>     '#type' => 'submit',
>     '#value' => t('Save'),
>     '#submit' => array('test_submit2'),
>
>   );
>
> Now, whether I click on any of them, the submit callback of second button is
> called always. But if I change the value of second button or give them
> different '#name', everything gets back in place. They call their submit
> functions respectively.
> I did not expect such a behavior given this:
> http://drupal.org/node/144132#buttons
> Although, they both have same #value but will have different ids in the
> form, so Drupal has every reason to differentiate between them (or not??).
> Why is such thing happening? Am I missing something?
> --
> Regards,
> Nitin Kumar Gupta
> http://publicmind.in/blog/
>


RE : Form with multiple submit buttons

by FGM :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Note that if the label on the buttons (#value property) is the same, FAPI will always return the last button: you need to have different #value on submits for op to be returned differently.
________________________________________
De : development-bounces@... [development-bounces@...] de la part de Ken Winters [kwinters@...]
Date d'envoi : mardi 6 octobre 2009 22:15
À : development@...
Objet : Re: [development] Form with multiple submit buttons

If you look at the HTML source, the submit button's name is "op" - in the comment form, this lets you switch whether to preview or post, for example.  The comment module is probably a good place to look at code, too.

- Ken Winters

On Oct 6, 2009, at 3:57 PM, nitin gupta wrote:

Hello,

I am having two form submit buttons on a single page, somethings like this:

$form['submit1'] = array(
    '#type' => 'submit',
    '#value' => t('save'),
    '#submit' => array('test_submit1'),

  );

[some more for elements in between]

  $form['submit2'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#submit' => array('test_submit2'),

  );


Now, whether I click on any of them, the submit callback of second button is called always. But if I change the value of second button or give them different '#name', everything gets back in place. They call their submit functions respectively.

I did not expect such a behavior given this:

http://drupal.org/node/144132#buttons

Although, they both have same #value but will have different ids in the form, so Drupal has every reason to differentiate between them (or not??). Why is such thing happening? Am I missing something?

--
Regards,
Nitin Kumar Gupta
http://publicmind.in/blog/