Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Drupal 6: Passing PHP variables to Javascript/jQuery and debugging with FireBug
In this tutorial I’ll show a way to pass PHP/Drupal variables to javascript/jquery using drupal_add_js(), and a way you can debug javascript variables using the FireBug console.
I’ll start by creating a hook_menu() implementation to establish a page callback:
Next I’ll define the page callback:
And here is the contents of the javascript include file I stuck in my module directory:
When viewing this page in a browser, you’ll see the following. The javascript popup window is using variables passed directly from a Drupal/PHP array:
The console.debug() javascript method was used to send data directly to the FireBug console. If you open FireBug, you’ll see the following:
If you click on the Object shown, you can drill into the variables further: