title: PHP code inside javascript
body: <p>I have no idea about how javascript works, but I was able to write the following code by copying from different sites.</p>
  
  <p>My aim is to have 2 radio boxes (1 = India, 2 = Other than India) and if Other than India is selected, then a dropdown box is displayed which shows names of all countries.</p>
  
  <p>The dropdown is selected from a database and it is achieved by a custom php function.</p>
  
  <p>I am able to make the code display the dropdown based on my radio box selection.</p>
  
  <p>What I am unable to do is as follows:</p>
  
  <ol>
  <li>I am not able to select any values from the dropdown.</li>
  <li>I'm not able to make the dropdown hide if I change the choice in the radio box</li>
  </ol>
  
  <p>Here is the code of the form:</p>
  
  <pre><code>&lt;form action="&lt;?php $_SERVER['PHP_SELF']?&gt;" method="post"&gt;
      &lt;fieldset&gt;
          &lt;br /&gt;
  
          &lt;script type="text/javascript"&gt;
              function onclickradio(entry){
                  if (entry === true) {
                      alert("YOU HAVE CHOSEN INDIA");
                  }
                  else { 
                      document.getElementById('OTHER THAN INDIA').innerHTML = '&lt;/br&gt;&lt;?php dropdown('country');?&gt;';
                  }
              }
          &lt;/script&gt;
  
          Country: &lt;/br&gt;
          &lt;input onclick="onclickradio(document.getElementById('INDIA').checked);" id="INDIA" name="country" type="radio" checked="checked"/&gt; INDIA
          &lt;input onclick="onclickradio(document.getElementById('INDIA').checked);" id="OTHER THAN INDIA" name="country" type="radio"/&gt; OTHER THAN INDIA
  
          &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
  
          State: &lt;input type="text" name="State" maxlength="30"/&gt;&lt;br /&gt;
          Line1: &lt;input type="text" name="Line1" maxlength="50" /&gt;&lt;br /&gt;
          Line2: &lt;input type="text" name="Line2" maxlength="50" /&gt;&lt;br /&gt;
          City: &lt;input type="text" name="City" maxlength="40" /&gt;&lt;br /&gt;
          PIN Code: &lt;input type="text" name="PIN_Code" maxlength="8" /&gt;&lt;br /&gt;
          &lt;input type="submit" name="submit_address" value="Submit Address" /&gt;
      &lt;/fieldset&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here is the code for the custom PHP dropdown function:</p>
  
  <pre><code>&lt;?php
      function dropdown($tablename) /*remember to add single quote around the input*/ 
      {
          $sql="SELECT * FROM ".$tablename;
  
          $result=mysqli_query($GLOBALS["connection"], $sql)
          or die('Error in running SELECT query');
  
          $options=""; //initialising the variable, so that it can be concatenated later
  
          while ($row=mysqli_fetch_array($result))
          {
              $x=0;   /*$x is the index of the field in a row (it has to start with $x=0;
                      because the first index is 0 in php*/ 
  
              $rowstr=" # ";  /*initialising the variable,
                              so that it can be concatenated later*/
  
              while ($x+1&lt;=mysqli_num_fields($result))    /*mysqli_num_fields gives the actual number of fields, and not the index. 
                                                          Since the index starts with 0, it is to be incremented by 1 
                                                          before comparison with the mysqli_num_fields*/ 
              {
                  $rowstr.= $row[$x]." # "; //concatenation operator
                  $x=$x+1;
              }
              $options.="&lt;option value=".$rowstr."&gt;".$rowstr."&lt;/option&gt;"; //concatenation operator
          }
  
          Echo "&lt;select&gt;".$options."&lt;/select&gt;";
      }
  ?&gt;
  </code></pre>