title: Passing php database result to javascript array
body: <p>I have to create a dropdown list and a button that will add another dropdownlist once click. i used this javascript code:</p>
  
  <pre><code>var array = ["Volvo","Saab","Mercades","Audi"];
  
          var cell2 = row.insertCell(1);
          var element2 = document.createElement("select");
          element2.name="activity_dropdown"
          element2.id="activity_dropdown"
          cell2.appendChild(element2);
  
          for (var i = 0; i &lt; array.length; i++) {
              var option = document.createElement("option");
              option.setAttribute("value", array[i]);
              option.text = array[i];
              element2.appendChild(option);
          }
  </code></pre>
  
  <p>but i want that my var array will come from the database result query. what can i do to pass php array to javascript array?</p>
  
  <p>by the way.. i have tried to used json_encode but it seems not working.. here is the my code:</p>
  
  <p>for data.php</p>
  
  <pre><code>    &lt;?php
      include('connection/dbConn.php');
  
  
  // get data and store in a json array
  $activity=$conn-&gt;query("SELECT activity_name FROM rehab_activities");
  
  
  while ($row =mysqli_fetch_array($activity)) {
      $activities[] = array(
      'actvityName' =&gt; $row['activity_name']
      );
  }
  
  echo json_encode($activities);
   ?&gt;
  </code></pre>
  
  <p>and try it here, but nothing comes out:</p>
  
  <pre><code>&lt;script&gt;
   jQuery(document).ready(function(){
      jQuery("#add").click(function(){
      var res = new Array(); 
          jQuery.getJSON("data.php", function(data) {
      var i= 0;
      while(i&lt;data.myarray.length){
                  res[i]=data.myarray[i];
               i++;
      }
    jQuery("#result").html(res[0]);
              });
          });
  
      });
  &lt;/script&gt;
  &lt;body&gt;
      &lt;input type="button" id="add"&gt;
      &lt;div id="result"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>