0

I have a form with only a submit button in it and I want to display an image in a new pop up window whenever form submit button is clicked in Drupal 7 node.

function custommodule_my_form($form, &$form_state) {
$imgurl = $form_state['build_info']['args'][1];
$form['submit'] = array(
    '#value' => 'Print Coupon',
    '#type' => 'submit',
);
return $form;
}

The image is present in the loaded node and I already have its URL inside a variable $imgurl.

Now how can add an attribute in this form so that $imgurl opens up in a new window whenever this submit button is clicked.

$form['submit']['#attributes']['onclick'] = ??   
0

You can give like this:

$form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
        '#attributes' => array(
            'onclick' => 'function_name()'),
        )
    );
| improve this answer | |
0

I would inject a bit of js code to listen to your click and show the popup with the image. You can open the popup url before the page reloads or after.

You may want to have a url path to a page that shows the image so that it will not get downloaded. e.g.

http://yoursite.com/showcoupon/[unique id of coupon]
| improve this answer | |
0

I resolved it like this:

 $form['submit']['#attributes']['onclick'] = "javascript: newwindow=window.open('$imgurl','height=500,width=300');";

So, the complete form code now becomes:

$form['submit'] = array(
    '#value' => 'Print Coupon',
    '#type' => 'submit',
);
$form['submit']['#attributes']['class'][] = 'form-submitone';
$form['submit']['#attributes']['onclick'] = "javascript: newwindow=window.open('$imgurl','height=500,width=300');";
return $form;
| improve this answer | |

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.