0

I've followed this : how to send variable from .php file to .js file?.

But following my code isn't working

What I want to do is whenever anybody logs in I want to send the login/password to browser javascript which will send it to another server.

It's only for a test case and not for production environment.

Details:

cyberoam.js

(function($) {
  Drupal.behaviors.cyberoam = {
    attach: function (context, settings) {

        alert(Drupal.settings.cyberoam.login) //on commenting this below alert works

      alert("hello");
    }
  };

})(jQuery);

In the cyberoam.module

function cyberoam_from_alter_submit(&$form, &$form_state)
{
     $username = $form_state['values']['name'];
     $password = $form_state['values']['pass'];

     dsm($username.':'.$password); //working fine

     drupal_add_js(array('cyberoam' => array('login' => $username )), 'setting');
}

What could be the problem? I've read same solution in multiple places but mine isn't working. In the Firebug console it shows TypeError: Drupal.settings.cyberoam is undefined

2

1 Answer 1

0

You shouldn't set the values in the submit hook, you should use hook_init().

In the submit hook you should set a variable ( variable_set("name", "value") ) and read it in the hook_init using variable_get("name", "dafault_value");

On the other hand, you should NEVER add the password in js variables. It's a huge security risk.

3
  • It's a test case. I want both login/passwd to be sent to the javascript. hook_init is called before hook_submit during which I'd not have collected the passwd
    – AgA
    Commented Jun 3, 2014 at 9:29
  • Probably you can use preprocess_page instead of hook_init Commented Jun 3, 2014 at 9:35
  • hook_init is called first but after hook_submit is called, the page will refresh. So if you save you values in variables using variable_set() they will be available when page refreshes. Hope it makes sense. Commented Jun 3, 2014 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.