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);

Comments

I've started implementing this technique, but I'd like to know how to pass variables from Javascript to the variables table without refreshing the page. Any ideas?

You can do this using AJAX

 

Cheers

Thanks, its nice, i want the one that will not refresh the page

If I write only one element:

drupal_add_js('mymodule' => 'Mike', 'setting'); 

It works ok but no with more atributes. All the data is correct in the DOM but I can not get the values. 

I don't no what could be wrong.

Subscribe to All Posts - Wesley Tanaka