2

I have a custom form in Drupal 6 and every thing works perfectly except I want the form submit to open in new window.
I tried using the following code as per form api for drupal 6 but it does not work

$form['save_and_print'] = array(
      '#type' => 'submit',
      '#value' => t('Save and Print'),
          '#attributes' => array('target' => '_blank'), // this does not work
);

Was anyone able to achieve this? Please help!

7

You have to set the target attribute in the form tag itself:

$form['#attributes']['target'] = '_blank';

Put this code somewhere in your form function.

Update:

To get this functionality to a particular button, you have to be tricky.

$form['save_and_print'] = array(
      '#type' => 'submit',
      '#value' => t('Save and Print'),
      '#attributes' => array('onclick' => 'this.form.target="_blank";return true;'),
);

When clicked, this will set the parent form element's target attribute to _blank which opens in a new tab when pressed. If you clicked the button once and when you click some other button in the same form, it will open in a new tab as well because the previous onclick event set the target attribute to _blank.

To workaround, you have to set this onclick event to ALL submits.

To submit the form in same page/tab:

$form['save'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#attributes' => array('onclick' => 'this.form.target="_self";return true;'),
);

To submit in a new tab:

$form['save_and_print'] = array(
      '#type' => 'submit',
      '#value' => t('Save and Print'),
      '#attributes' => array('onclick' => 'this.form.target="_blank";return true;'),
);

demo

  • 1
    thanks for your time! :) But that will be applicable for all submits in that form. I wanted it work for selected ones. :( – NewBie Feb 25 '13 at 6:43
  • You'll have to go for javascript then. – AyeshK Feb 25 '13 at 15:54
  • 3
    See the updated answer. – AyeshK Feb 25 '13 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.