views:

34

answers:

2

Hello,

I have set theme handler for a form in Drupal and this handler has a template file. What is the easiest way to send a value from PHP to Javascript in that include file?

Thank you.

A: 

Easiest way is just to echo it out into a script tag.

<script type="text/javascript">
   var variableName = <?php echo $variable ?>;
</script>

If Drupal uses a templating language, you need to replace the <?php echo... ?> with the relevent output method...

Andru
also if variable is a string you should put it in a quotes and escape if needed.
kodisha
+2  A: 

The standard way to do this in Drupal is with drupal_add_js, using the "setting" type, e.g.:

drupal_add_js(array('variableName' => 'value'), 'setting');

That will make the variable available on the JS side at Drupal.settings.variableName.

Scott Reynen