Drupal Technical
[Drupal] How to pass values from PHP to javascript in a Drupal site?
Are you looking for a way manipulate dom elements using javascript based on some values from PHP, then this article is for you. We had recently worked in a Drupal 6 site having the same scenario. Our requirement was to enable some checkboxes using javascript based on the values of some variables available in the backend, This was accomplished using Drupal.settings
Steps
First inside custom module, create an array to be passed to Drupal.settings.
<?php
// Assiging the value to a variable
$variable_name = 'value';
$settings_name = array (
'my_custom_settings' => $variable_name,
);
// Call to drupal_add_js and the parameters are our custom settings as array, and second is 'settings' itself.
drupal_add_js(array('custom_settings' => $settings_name), 'setting');
?>
So we have created our custom settings and the variable in our custom settings array will be available in js. We can access this value in js file like this.
var customVariable = Drupal.settings.custom_settings.my_custom_settings;
Hope this helps.
Reference: