Hi,

I have a form that has two buttons, one for next and one for previous - how do I detect which has been clicked?

Form function

	$form['previous'] = array(
	  '#type' => 'submit',
	  '#value' => t('Previous'),
	);	
	$form['submit'] = array(
	  '#type' => 'submit',
	  '#value' => t('Next'),
	);	

Submit Function

	if($form_state['#submit']['previous'])
		drupal_goto("signup/1");
	else
		drupal_goto("signup/3");

Many thanks

Comments

pwhite’s picture

Just if it helps anyone I added a #validate into the first button:

	$form['previous'] = array(
	  '#type' => 'submit',
	  '#value' => t('Previous'),
	  '#validate' => array('signup_form_previous'),
	);	
	$form['submit'] = array(
	  '#type' => 'submit',
	  '#value' => t('Next'),
	);

That called the function signup_form_previous with a drupal_goto function in.

si_odong’s picture

found your answer by dumping $form variable on hook form validate. there's an op object that show you which submit triggered.

Mikey Bunny’s picture

Use the '#submit' value to specify a dedicated form submit handler for each button.

$form['previous'] = array(
  '#type' => 'submit',
  '#value' => t('Previous'),
  '#validate' => array('signup_form_previous'),
  '#submit' => array('signup_form_previous_submit'),
);

d0minar’s picture

Does anyone have a solution to this?

jaypan’s picture

if($form_state['values']['op'] == $form_state['values']['previous'])
{
  // back button was clicked
}
elseif($form_state['values']['op'] == $form_state['values']['submit'])
{
  // next button was clicked
}

Contact me to contract me for D7 -> D10/11 migrations.

Mikey Bunny’s picture

See my suggestion above.