I have a form and on that form the #submit action it calls a function that saves some data. Now what I need to do is I need to pass some data back to the website confirm page to be used by JavaScript. To do this I used drupal_add_js() 'settings' but this does seem to work? The data never gets set.
From what I've read I think it might be something with the form redirecting during the submission process and the JS is getting lost?
-
i think it's setting not settings– rémyJul 9, 2012 at 21:51
-
No I mean "settings" as in the method of adding JS api.drupal.org/api/drupal/includes%21common.inc/function/…– OwenJul 10, 2012 at 6:48
-
1yes - but that's without s ;)– rémyJul 10, 2012 at 8:19
4 Answers
it might be something with the form redirecting during the submission process and the JS is getting lost?
Yes, you are absolutely right! Normaly, after form submitting user is redirected with drupal_goto()
function. Messages which set with drupal_set_message()
are stored in $_SESSION so they are not lost, but drupal_add_js()
doesn't use session.
You can prevent redirect in your submit function:
// In this case your form hanler will be able to see flag $form_state['submitted'] and
// other data from previous form processing to indicate that form was submitted.
$form_state['rebuild'] = TRUE;
// Will work.
drupal_add_js(array('myModule' => array('test' => 'test-value')), 'setting');
Or with:
$form_state['redirect'] = FALSE;
// Will work, too
drupal_add_js(array('myModule' => array('test' => 'test-value')), 'setting');
But I would have used $_SESSION
as it is more flexible.
$_SESSION['js_settings'] = array('myModule' => array('test' => 'test-value'), 'setting');
Then you have to process $_SESSION
yourself in accordance with your site logic.
if you have a form attaching js or css could be more painless with
'#attached' option this can attach css js library and settings documentation here
you may use hidden form element to save some data it will always appear on form_states. Setting some values to hidden field is easier solution IMHO
You can set a session in form submit:
function my_form_submit($form, $&state) {
$_SESSION['js_value'] = 5;
}
Then you can check if this session variable is set in your theme's template.php file and theme_preprocess_page()
hook. This hook is executed everytime you load a page (and only after form redirect, not before), so you can add the js variable into your html here. But do not forget to unset the session variable after adding js varible, otherwise it will be loaded on every page request, not only after form submit.
function theme_preprocess_page($&vars) {
// check if the session variable exists
if (isset($_SESSION['js_value']) {
drupal_add_js(array('myModule' => array('js_value' => $_SESSION['js_value'])), 'setting');
}
// optionally you can add a js file that will process the variable (if not added by default)
drupal_add_js('path/to/js/file.js');
// do not forget to unset the session variable
unset($_SESSION['js_value']);
}
You can then access the variable in file.js, that will be loaded after form submit:
jQuery(document).ready(function () {
// check if the variable exists and finally do some action
if(typeof Drupal.settings.myModule.js_value != 'undefined') {
alert(Drupal.settings.myModule.js_value);
}
});
This way your js variable will be present in any page and only at the page that comes first after the form submit.
In the submit hook you should set a variable ( variable_set("name", "value")
) and read it / add it to js settings in the hook_init() using variable_get("name", "dafault_value")
;
Please take into consideration that saving variables using variable_set() will save them to the database. You should delete them if you don't need them anymore. If the value you want to save will be used one time only you might want to use sessions instead.