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');
}
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',
  );
}
  • But the script will also load on page load. I just need it to load if the form is submitted.. – user3384012 Aug 19 '14 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. – Aneek Mukhopadhyay Aug 19 '14 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, privacy policy and cookie policy

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