Hi,

Using Forms API, how to create a button (not submit) to call a function or to redirect the user? I understand the submit and it works fine but could not figure out how to create a button that performs an action like redirect or call a function?

Thanks and Regards

Comments

Jaypan’s picture

They are one and the same. You create the submit button, and in the submit function you set your action (redirect/call a function).

I am learning’s picture

It means I have two

<input type="submit" name = "update" value="Update">
<input type="submit" name = "cancel" value="Cancel">

instead of

<input type="submit" name = "update" value="Update">
<input type="button" name = "cancel" value="Cancel">

Did I get it right?

Jaypan’s picture

Yes (though you should show your form API definition, not the HTML).

Then you will check which one was clicked in your $form_state['values']['op'], and perform the action accordingly.

I am learning’s picture

function ca_designation_edit() {
  ////////////////////////////////////////////////////////////
  // Update designation
  
	$path = drupal_get_path_alias($_GET['q']);
	$path = explode('/', $path);
	$DesignationID = array_pop($path);
	
	$selectsql = 'SELECT Designation from {ca_designation} where DesignationID = %s';
	$designation = db_result(db_query($selectsql, $DesignationID));
	
	$form['#attributes'] = array('enctype' => 'multipart/form-data');
	
	$form['fieldset'] = array(
	'#type' => 'fieldset',
    '#title' => t('Update record'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    'updatehelp' => array(
      '#prefix' => '<div>',
      '#value' => t('Editing this record is not recommended, you should import a CSV generated from the off-line version if you feel you need updated data.'),
      '#suffix' => '</div>',
    ));
	
	$form['fieldset']['designation'] = array(
    '#type' => 'textfield',
    '#title' => t('Designation'),
    '#size' => 30,
    '#maxlength' => 100,
	'#default_value' => $designation,
    '#description' => t('Designation'),
	'#required' => TRUE
    );
	
	//Store the designation id got from the query string in a hidden field
	$form['designation_id'] = array(
    '#type' => 'hidden',
	'#value' => $DesignationID,
    '#description' => t('DesignationID through the query string')
    );
	
	//Create a Cancel Button
	$form['fieldset']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#name' => 'cancel',
    '#submit' => array('ca_designation_list'),
    );



	
    $form['fieldset']['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Update',
    );
    return $form;
}
Jaypan’s picture

This:

    $form['fieldset']['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Update',
    );

Should be this:

    $form['fieldset']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Update'),
    );

Then in your submit function, you would do this:

if($form_state['values']['op'] == t('Update'))
{
  // do your update stuff
}
elseif($form_state['values']['op'] == t('Cancel'))
{
  // do your cancel/redirect stuff
}

Also, in the future, when posting code, try to scale it down to the relevant code. No code is no good, but so is too much code! In this case it wasn't a big deal, but other times it will reduce your chances of getting a response, as people often don't want to dig through lines and lines of code to find the relevant parts. If you haven't provided enough, people will ask for more.

I am learning’s picture

I thought it will help, I'll be specific next time :-)

I'm just checking your suggestion

I am learning’s picture

canceling this returns back to the forms


  if($form_state['values']['op'] == t('Update'))
  {
    //update
  }
  elseif($form_state['values']['op'] == t('Cancel')) {
    drupal_goto('admin/content/ca_designation/list');
  } 

I checked the $form_state['values']['op'] but didn't find key 'op'

Array
(
    [designation] => Principal
    [cancel] => Cancel
    [submit] => Update
    [designation_id] => 1
    [form_build_id] => form-52c6ed2d8250015d9116aa76240dcf62
    [form_token] => 669f08d7ba625ffefb9cc9036eae8717
    [form_id] => ca_designation_edit
)
Jaypan’s picture

Whatever you are showing there, it's not $form_state['values'] - the form_id is not part of $form_state['values'].

I am learning’s picture

This is the output of the following I tried:

function ca_designation_edit_submit($form, &$form_state) {
  echo '<pre>';print_r($form_state['values']);echo '</pre>';exit;

Is my implementation is wrong?

Jaypan’s picture

No, it turns out I was wrong - the form_id is a part of $form_state['values']. But so is $form_state['values']['op']. Did you configure it as type 'submit' or type 'button'?

I am learning’s picture

Submit, as you suggested.

Jaypan’s picture

Show your FAPI code again then.

I am learning’s picture

//Create a Cancel Button
    $form['fieldset']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#name' => 'cancel',
    );



	
    $form['fieldset']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Update'),
    );
Jaypan’s picture

Try removing this:

'#name' => 'cancel',
Then check again whether 'op' is set or not.

Also, do you have any javascript set on this form that is set to submit the form?

I am learning’s picture

Removing this worked, and I got 'op' as well, Thank you so much.
Regards

Jaypan’s picture

No problem. Glad it worked!

duckzland’s picture

something like this perhaps?

function my_dummy_form($form_state) {
 	$form['edit'] = array(
		'#type'  => 'submit',
		'#name' => 'edit',
		'#value' => t('Redirect ME'),
		'#submit' => array('edit_content'),
	);
return $form;
}
function edit_content($form, &$form_state) {
    drupal_goto('node/nid');
}

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

I am learning’s picture

//Create a Cancel Button
	$form['fieldset']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#name' => 'cancel',
    '#submit' => array('ca_designation_list'),
    );



	
    $form['fieldset']['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Update',
    );

Clicking on Cancel doesn't invoke this function and it remains on the form.

duckzland’s picture

What do you want to achieve for the cancel button should be coded in


function ca_designation_list($form, $form_state) {

// cancel code / logic goes here

}

essentially you got 2 different button, one is the "cancel" and the other is the "submit" button, the "cancel" button will look for the "ca_designation_list" function and the "submit" button will search for the _submit function (because you haven't specified special '#submit' value in the form array)

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

I am learning’s picture

This button will be on a Form to edit the existing record, so cancel will take the admin / user back to the listing page. But it can be for any purpose e.g. delete the current record, so call a function to perform so. I'm not able to call a function on a button. An example will be a great help.

Thanks

I am learning’s picture

Thanks Jay, you have been a great help.

This one is for anybody looking for the same, this is how it is implemented (Courtesy Jay)

In you form:

	//Create a Cancel Button
	$form['fieldset']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    );
	
    $form['fieldset']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Update'),
    );

and in your submit:

if($form_state['values']['op'] == t('Update'))
  {
    //Update Statement(s) or whatever
  }
  elseif($form_state['values']['op'] == t('Cancel')) {
    drupal_goto('ur path'); // OR Whatever
  } 

Thanks Jay.