Hello All,
Please tell how can i add "Cancel" button in user registration form?

I have added it in function user_register() {} function, but the cancel button is moving above the user registration field set, and "Create New Account" button is below the fieldset.
How can place these two i one line, below the fieldset.

I have added prefix and suffix aswell.

$form['submit'] = array('#type' => 'submit', '#value' => t('Create New Account'), '#weight' => 30, '#prefix' => '<div class=userregButton>');

$form['cancel'] = array('#type' => 'button', '#value' => t('Cancel'), '#suffix' => '</div>' );

Please help.

Comments

Aran78’s picture

Hi,
i had a similar problem, and i have solved using the type 'markup' and inserting a html button and the "onclick" javascript function for a callback.

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Sned'),
);
$form['cancel'] = array(
'#type' => 'markup',
'#value' => ''
);

The 2 button now are on the same line.
I hope this will be usefull.
If anyone has a better idea, write here :-D

Aran

kendre_paresh’s picture

Nooo,
It simply give me the label "Cancel", and again on the top of the page, not beside the "Create New Account".

I have done in this way

$form['cancel'] = array('#type' => 'markup', '#value' => t('Cancel'), );

Thanks n regards,
Paresh Kendre.

j_ten_man’s picture

Try giving it a weight as such:


$form['cancel'] = array('#type' => 'button', '#value' => t('Cancel'), '#suffix' => '</div>', '#weight'=>30,);
kendre_paresh’s picture

Great,

It works for me, thank you so much! :)

Thanks n regards,
Paresh Kendre.

yogitha’s picture

Can any one help me out i need 'Cancel' button .
onclick cancel button-
1) session must destroy and
2) Redirect to home page ie drupa_goto()

For that i added below code its working fine for session destroy, but how could i redirect to home page ie drupal_goto()

$form['cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#attributes' => array('onClick' => session_destroy()),
'#weight' => 1002,
);

after this how could i redirect .
Thanks in advance.

kendre_paresh’s picture

have you tried this one?

$form['cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#attributes' => array('onClick' => 'session_destroy(); drupal_goto("pathOfIndexPage")'),
'#weight' => 1002,
);

try to add the actions in quotes separated with semicolon(;)

try it.

Thanks n regards,
Paresh Kendre.

yogitha’s picture

$form['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#attributes' => array('onclick' => 'window.location = '. base_path() .'; return false; ', session_destroy()),
'#weight' => 1002,
);
---------------------
i tried this ,and i am successful for both session close and redirect to front page.

kendre_paresh’s picture

ok.

Thanks n regards,
Paresh Kendre.

casperovich’s picture

Drupal 7 solution.

Work for me :)))

$form = array();
    $form['elem1'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#description' => t('Description'),
      '#required' => TRUE,
    );
    return confirm_form($form, '', 'admin/config', '', t('Save'), t('Cancel'));

Good Luck

nitin.k’s picture

    $form['actions']['cancel'] = [

      '#type' => 'submit',

      '#value' => $this->t('Cancel'),

      '#button_type' => 'primary',

      '#attributes' => [

        'onClick' => 'javascript:window.history.go(-1); return false;'

      ],

    ];