2011-11-14 13 views
5

Ho bisogno di fare questo, ma con forme Drupal:Come posso creare un pulsante Indietro con Drupal form API?

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

Ho provato a fare questo, ma non ha funzionato:

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

e anche:

$form['back'] = array(
    '#type' => 'button', 
    '#value' => 'Back', 
    '#attributes' => array(
    'class' => 'button-user', 
    'onclick' => 'location.href=edit',   
    )  
); 
+0

mai chiudere il primo con le virgolette '" ' – switz

risposta

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

funziona per me:. D thx –

+0

sei il benvenuto :) –

+0

Inoltre, non dimenticare di impostare questo come la risposta accettata :) –

0

altro la soluzione sarebbe questa:

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) { 
    switch($form_id) { 
    case "YOUR_FORM_ID": 

     unset($form['#validate']); //Maybe necessary 

     $form['actions']['back'] = array(
     "#type" => "button", 
     "#value" => t('Back'), 
     "#weight" => 15, 
     "#executes_submit_callback" => FALSE, 
     '#limit_validation_errors' => array(), 
     '#ajax' => array(
      'callback' => 'YOUR_MODULE_go_back_callback' 
     ) 
    ); 

     break; 
    default: 
     break; 
    } 
} 

function YOUR_MODULE_go_back_callback() { 
    $html = ' 
    <script type"text/javascript"> 
    window.history.go(-1); 
    </script> 
    '; 
    return $html; 
} 
3
$form['back']['#markup'] = "<input type='button' class='button-user' value='Back' onclick='window.history.go(-1)'/>"; 

Questo funziona per qualsiasi pagina.

1

Aggiungendo semplicemente la mia versione, che sembra funzionare benissimo in 7, basta prenderla nel ciclo di ricostruzione e reindirizzare invece. Estensibile, puoi aggiungere altri pulsanti per fare le cose, nota l'ortografia del valore "Indietro" è il nome dell'opzione (operazione) .. qualcosa che mi ha confuso e infastidito finché non l'ho capito.

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'); 
    } 
} 
+0

Grazie per aver risolto il problema "op" .Questo bugging me anche. – zkent

0

ho preferito una soluzione che ha richiesto nessun JavaScript. Simile alla risposta di Grizly, ma senza metterlo nella convalida del modulo, che è sembrato brutto. Ma il codice qui sotto offre un pulsante di collegamento.

function my_form(&$form, &$form_state) { 
    // Some form elements 

    // Regular submit button 
    $form['actions']['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Submit'), 
); 

    // Back button (different submit handler prevent the standard submit and takes us 
    // to the redirect-submit). 
    $form['actions']['back'] = array(
    '#type' => 'submit', 
    '#value' => t('Go back'), 
    '#submit' => array('custom_back_button'), 
); 
} 

// Custom form callback for redirection. 
function custom_back_button($form, &$form_state) { 
    $form_state['redirect'] = '/edit'; 
} 
1

opzione più semplice in Drupal API Forma, Uso #attributes opzione.

$form['back-btn'] = array(
    '#type'     => 'button', 
    '#value'    => t('Back'), 
    '#attributes'   => array('onclick' => onclick='window.history.back();'), 
); 
+1

La mia domanda era originariamente per drupal 6 4 anni fa ma penso che potrebbe essere utile per gli altri, grazie –

+0

$ form ['back-btn'] = array ( '#type' => 'button', \t \t '#value' => t ('Indietro'), '#attributes' => array ('onclick'=> 'window.history.back();'), ); – SushilKumar

0

Questo è il pulsante indietro che sto usando. Il "ritorno falso" evita l'invio del modulo.

$form['back'] = array(
    '#type' => 'button', 
    '#value' => t('<< Back'), 
    '#attributes' => array(
    'onclick' => 'window.history.back();return false;', 
), 
); 
Problemi correlati