Tuesday, July 26, 2011

Pass PHP variables to Javascript in Drupal 6

 To access  PHP values and variables from JavaScript or convert php variables to JavaScript variables using Drupal 6, you can send them to your JavaScript code using Drupal's drupal_add_js function as follows:
drupal_to_js - Converts a PHP variable into its Javascript equivalent.
 example -
$msg= 'hello dear';    //passing php variables to javascript

 drupal_add_js("var apath = " . drupal_to_js($apath) . ";", 'inline');

Drupal_add_js(
    '$(document).ready(function(){
                        alert(msg);               
                   })',
    'inline'
);

3 comments: