Drupal drupal_add_js/6.x: Passing PHP variables to Javascript/jQuery and debugging with FireBug

Avatar-eric-london
Created by Eric London on 2010-05-22
Tags:
New Comment
 
Please note: the content on this page orginates from ericlondon.com.
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:
<?php
function helper_menu() {

  $items = array();
  
  $items['js-vars'] = array(
    'title' => t('Javascript Variables'),
    'description' => t('Javascript Variables'),
    'page callback' => 'helper_page_callback_js_vars',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,    
  );
  
  return $items;

}
?>


Next, I'll define the page callback:
<?php
function helper_page_callback_js_vars() {

  // include module javascript file
  drupal_add_js(drupal_get_path('module','helper') . '/js/helper.js');

  // define variables you'd like to pass to the DOM
  $js_vars = array(
    'js_vars' => array(
      'message' => t('Hello @username', array('@username' => $GLOBALS['user']->name)),
      'an_array' => array(
        'color' => t('red'),
        'name' => t('Eric'),
      ),
    ),
  );
  
  // pass variables to javascript
  drupal_add_js($js_vars, 'setting');
  
  // generate some page output
  return "TEST";

}
?>


And here is the contents of the javascript include file I stuck in my module directory:

$(document).ready(function(){

  // debug variables directly in FireBug
  console.debug(Drupal.settings.js_vars);
  
  // popup mesage passed from Drupal
  alert(Drupal.settings.js_vars.message);
  
});


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:
javascript popup

The console.debug() javascript method was used to send data directly to the FireBug console. If you open FireBug, you'll see the following:
FireBug Console

If you click on the Object shown, you can drill into the variables further:
FireBug Console





Comments

 
  • Sorry to be so dense, but why
    Created by Anonymous on 2010-05-25
    Sorry to be so dense, but why is this better than print_r($whatever) or dvm($whatever) ?
    • good question
      Created by Eric.London on 2010-06-01
      Good question! This tutorial is used to debug javascript variables passed to the DOM, per AJAX, AHAH, etc.

      When I debug PHP, I use krumo(), print(), dsm(), etc :)
  • This doesn't work for me.
    Created by Anonymous on 2010-06-06
    I created a module with your two php functions and a separate js file in the module's js subdirectory. The page loads fine, showing the "TEST" output, but there is no js alert button. The js file shows up in the page source. Not sure what's wrong.
  • Re: This doesn't work for me.
    Created by Anonymous on 2010-06-06
    It has something to do with the alert box, as

    $("#msgid").html(Drupal.settings.js_vars.message);

    works.
  • Quick comment
    Created by Anonymous on 2010-06-06
    Thanks for this. Just a note that the array keys must not be numeric.
    • pass variable php to js as an object with json
      Created by Anonymous on 2010-11-30
      
      $var= array(
      'js_v' => array(
        'status' => true,
        ),
      );
      

      We also could pass the variable using json
      Example:
      
      print drupal_json($var);
      and retrieve the data with
        $.getJSON('url', function(datos)) {
          //and use it
          print(data.js_v.status);
          ....
       })
      


      -jota.
  • Javascript variables with AJAX
    Created by Anonymous on 2010-10-06
    Is it possible to add javascript variables with an ajax call?