Don’t worry! Drupal provides an easy way to do this using Drupal.settings.
Now first of all what is this Drupal.settings ?
Now question is how we can pass the data from PHP code ?
So what are we waiting for, lets take a look to an example:
drupal_add_js(array('your_module_name' => array( 'your_info_variable' => 'your_passed_info' )), 'setting');
drupal_add_js($data = NULL, $options = NULL)
For this we have to mention the proper parameter name ( which is ‘setting’ ) as the option at the last.
$your_passed_info = array( 'your_info_variable_1' => 'your_passed_info', 'your_info_variable_2' => 'your_passed_info' ... ... );
Now, we can use this info array as below:
drupal_add_js(array( 'your_module_name' => $your_passed_info ), 'setting');
var yourInfoVariable = Drupal.settings.your_module_name.your_info_variable; var yourInfoVariable1 = Drupal.settings.your_module_name.your_info_variable_1; var yourInfoVariable2 = Drupal.settings.your_module_name.your_info_variable_1;
Here you can see, we are using Drupal.settings for getting the passed data with the variable names which we have used in our PHP code.
HAPPY DRUPALIZING 🙂