I am trying to assign a php array to js.

// On the Server:

CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar'));

// In the JS file or inline script

CRM.alert(CRM.vars.myNamespace.foo); // Alerts "bar"

It shows me a following error

TypeError: CRM.vars.myNamespace is undefined

  • 1
    I dont know the answer, but I would try to see what would happen if you json_encode the assigned array. Or another debugging is rather than do a alter. You could do a console.log(CRM.vars) and then you can see what is available in the CRM.vars object when you open the developer screen of your browser (in firefox press f12). – Jaap Jansma - CiviCooP Feb 20 '19 at 7:10
  • 1
    What server file and what js file are you putting these in? They have to both execute on the same page request. – Demerit Feb 20 '19 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.