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