Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a back button with Drupal form API?

I need to do this but with Drupal forms:

<input type="button" class="button-user" value="Back" onclick="location.href='edit'"/>

I tried doing this but it did not work:

$form['back']['#prefix'] = "<input type='button' class='button-user' value='Back' onclick='location.href='edit''/>;

and also:

$form['back'] = array(
  '#type' => 'button',
  '#value' => 'Back',
  '#attributes' => array(
     'class' => 'button-user',
     'onclick' => 'location.href=edit',        
   )       
 );
like image 745
Gonzalo Moreno Caballero Avatar asked Nov 14 '11 20:11

Gonzalo Moreno Caballero


4 Answers

$form['back']['#markup'] = "<input type='button' class='button-user' value='Back' onclick='location.href=\'edit\''/>";
like image 57
Marius Ilie Avatar answered Oct 23 '22 00:10

Marius Ilie


$form['back']['#markup'] = "<input type='button' class='button-user' value='Back' onclick='window.history.go(-1)'/>";

This works for any page.

like image 40
VivMajor Avatar answered Oct 22 '22 23:10

VivMajor


Just adding my version, which seems to work great in 7, just catch it in the rebuild cycle and redirect instead. Extensible, can add any other buttons to do things, note the spelling of value "Back" is the name of the "op" (operation).. something that confused and annoyed me until I figured it out.

function mymodule_something_form($form,&$form_state){

    //... Rest of form

    $form['unused_form_id_back'] = array(
        '#type' => 'button',
        '#value' => 'Back',
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Do Stuff!'
    );
    return $form;
}

function mymodule_something_form_validate($form, &$form_state)
{
    if($form_state['values']['op'] == 'Back'){
        drupal_goto('something/that_page');
    }
}
like image 36
Grizly Avatar answered Oct 22 '22 22:10

Grizly


Simplest option in Drupal Form API, Using #attributes option.

$form['back-btn'] = array(
    '#type'                 => 'button',
    '#value'                => t('Back'),
    '#attributes'           => array('onclick' => onclick='window.history.back();'),
);
like image 24
Ashwin Parmar Avatar answered Oct 22 '22 22:10

Ashwin Parmar