Hi,

I am trying to do something very simple and I can't find the way to do it.
I want to create a form with one button "Print" that when pressed will open the windows print dialog.

I created a javascript file called printer_window.js
I added the javascript file using the module hook_init like this

function my_module_init() {
	drupal_add_js(drupal_get_path('module', 'my_module') . '/printer_window.js');
}

function my_form_submit($form, &$form_state) {

}

My javascript function name is OpenPrintWindow
My question is how do I call the Javascript function from function my_form_submit?

I spent the last 2 days looking for an example and could not find anything. The drupal documentation about javascript is pretty confusing

can someone please help?

Comments

goldkey266’s picture


function my_form($form_state,$nid,$sid){

	$jslink = "PrinterTool.print";

        $form['print'] = array(
		'#type' => 'submit',
		'#value' => t('Print'), 
		'#access' => user_access('print code'),
		'#attributes' => array('onclick' => $jslink),           <-- This is the trick that did it!!!!
		'#executes_submit_callback' => FALSE               <-- And this one prevents the form from being submitted
		);

	return $form;
}
sri20198’s picture

I tried what you have done. But clicking submit button reloads the page. Looking at the source, the form action is getting set to the present page. And even if I set form['#action'] = '', nothing is changing.

Am I missing anything?

Regards,
Sid

jaypan’s picture

you need to return a false value from your javascript function. You will do that inside the javascript script.

Contact me to contract me for D7 -> D10/11 migrations.

sri20198’s picture

Thanx Jay. I didnt think of the return false option.

However, I couldnt set the return false from the JavaScript. But I succeeded in setting the return false from within the module itself. Something like this:

$form['#attributes'] = array('onsubmit' => 'return false');
aerozeppelin’s picture

Thank you for posting this. I spent hours trying to figure this out.