Body: <p>I am trying to assign a php array to js.</p>
  
  <p>// On the Server:</p>
  
  <pre><code>CRM_Core_Resources::singleton()-&gt;addVars('myNamespace', array('foo' =&gt; 'bar'));
  </code></pre>
  
  <p>// In the JS file or inline script</p>
  
  <pre><code>CRM.alert(CRM.vars.myNamespace.foo); // Alerts "bar"
  </code></pre>
  
  <p>It shows me a following error</p>
  
  <blockquote>
    <p>TypeError: CRM.vars.myNamespace is undefined</p>
  </blockquote>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-20T07:10:39.513Z
Comments.Text: 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).
Comments.UserDisplayName: Jaap Jansma - CiviCooP
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-20T12:16:08.057Z
Comments.Text: What server file and what js file are you putting these in? They have to both execute on the same page request.
Comments.UserDisplayName: Demerit
Tags: <extension><php><javascript>
Title: Assigning a php associative array to JS?
ThreadText: # Assigning a php associative array to JS?
  
  (Asked by: username_0 on 2019-02-20)
  
  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
  
  **Tags:** extension, php, javascript
  
  > **Comments:**
  > **username_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).  
  > **username_2**: What server file and what js file are you putting these in? They have to both execute on the same page request.  
  
  ---
  
  ## 0 answers
  
  