Exchanging variables between PHP and JavaScript
<p>The selected radio button will show its corresponding list of cities by a dropdown box.
For example, upon the selected radio button ‘Ontario’, a dropdown box with matching cities will show up (in this example the ‘Region’ is Ontario and the id is 77).</p>

<p>All PHP functions, loops and the JavaScript code mentioned below in the example, are working correctly:</p>

<pre><code>&lt;script type="text/javascript"&gt;
$(document).ready(function(){
  $('#locationTree input:radio').change(function() {
    var buttonPressed = $('input:radio[name=Region]:checked').val();

    if(buttonPressed == 77) {
      var listCities = $("select[name='City']");
      &lt;?php View::newInstance()-&gt;_exportVariableToView(77); ?&gt;
      &lt;?php while(has_list_cities()) { ?&gt;
        $("&lt;option&gt;&lt;?php echo list_city_name(); ?&gt;&lt;/option&gt;").appendTo(listCities);
      &lt;?php } ?&gt;                
    } 

  });   
});
&lt;/script&gt;  
</code></pre>

<p>To get list of cities by different regions and not specifically to 77, I can slightly change the PHP code as follows:</p>

<pre><code> &lt;?php View::newInstance()-&gt;_exportVariableToView($regionCode); ?&gt;
</code></pre>

<p>and since the variable ‘buttonPressed’ detects the selected id of ‘Region’, I can add the following simple script:</p>

<pre><code>&lt;?php $regionCode = "&lt;script&gt;document.write(buttonPressed)&lt;/script&gt;"; ?&gt;   
</code></pre>

<p>After the above changes:</p>

<pre><code>var listCities = $("select[name='City']");
&lt;?php $regionCode = "&lt;script&gt;document.write(buttonPressed)&lt;/script&gt;"; ?&gt;   
&lt;?php View::newInstance()-&gt;_exportVariableToView($regionCode); ?&gt;
</code></pre>

<p>I know PHP is run server side and JavaScript is run client side but I thought that my simple solution for exchanging variables between PHP and JavaScript should work… but it doesn’t work. I really ask someone help me get it working.</p>
