1

I have alter and hook submit for webform. I would like to pop a modal if form is successfully submitted. But drupal_add_js seemed to be not working inside the hook submit.

function myModule_form_alter(&$form, &$form_state, $form_id) {

    if($form_id == 'webform_client_form_9') {
        $form['#submit'][] = 'myForm_submit';
    }

}

function myForm_submit($form, &$form_state) {
    drupal_add_js(drupal_get_path('module', 'myModule') . '/misc/custom.js');
}

3 Answers 3

1

You should do this with #attached.

Like,

if (!empty($form_state['values'])) {
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'ajax_example') . '/ajax_example.css',
  );
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
  );
}
2
  • But the script will also load on page load. I just need it to load if the form is submitted.. Commented Aug 19, 2014 at 6:14
  • Yes, I just gave you one snippet. This has to be wrapped in a condition to check whether the form is submitted. Edited my answer. Commented Aug 19, 2014 at 10:11
0

Aneek Mukhopadhyay is on the right track but if you need it only for submit you should add the code inside an IF statement:

if( isset($form_state["values"]) ) {
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'ajax_example') . '/ajax_example.css',
  );
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
  );
}

Hope it helps.

-1

I think so a javascript file is added when a page loads.In this case page loads after hook_submit gets called.Why dont you try to add the js the time a page loads in hook_init and try.

Thanks, Keep calm and clear the cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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