wesley tanaka

Pass PHP variables to Javascript in Drupal 6

‹ How to tell it's spring, method #734 | Olfactory SMS ›
()

To access PHP values and variables from Javascript using Drupal, you can send them to your javascript code using Drupal's drupal_add_js as follows:

drupal_add_js(array('mymodule' => array(
'username' => 'Mike',
'uid' => 123,
'likes' => array('ice cream', 'peanuts', 'coffee'),
)), 'setting');

Then in Javascript, you can use your variables which will appear inside Drupal.settings:

alert(Drupal.settings.mymodule.username);

Suggested Links

Syndicate content