title: how to make javascript works after php database query?
body: <p>I am using bootstrap framework, javascript(jquery) and php for my website.
  So basic I want to press a button, by pressing it, we query database and return result, the results will be displayed in the bootstrap javascript powered modal: because popup modal is written in javascript and its run after php, the $row is always shown as undefined. I am wondering if there is a way to make php runs before js?</p>
  
  <pre><code>&lt;form action="" method="post"&gt;
  &lt;input type="text" id="field" name="field"&gt;
  &lt;button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" name="submit" id="submit"&gt;
  Launch demo modal
  &lt;/button&gt;
  &lt;/form&gt;
  &lt;?php
  if(isset($_POST['submit'])){
      $link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " .     mysqli_error($link)); 
  
     //consultation: 
  
     $query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link)); 
  
     //execute the query. 
  
     $result = mysqli_query($link, $query); 
  
     //display information: 
  
     if ($row = mysqli_fetch_array($result)) { 
  
  
  
     ?&gt;
  
      &lt;!-- Modal --&gt;
      &lt;div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt;
       &lt;div class="modal-dialog"&gt;
        &lt;div class="modal-content"&gt;
         &lt;div class="modal-header"&gt;
            &lt;button type="button" class="close" data-dismiss="modal"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;span class="sr-only"&gt;Close&lt;/span&gt;&lt;/button&gt;
            &lt;h4 class="modal-title" id="myModalLabel"&gt;Modal title&lt;/h4&gt;
        &lt;/div&gt;
       &lt;div class="modal-body"&gt;
      &lt;?php echo $row["name"] . "&lt;br&gt;"; ?&gt;
    &lt;/div&gt;
    &lt;div class="modal-footer"&gt;
      &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
      &lt;button type="button" class="btn btn-primary"&gt;Save changes&lt;/button&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>
  </p>
  
  <pre><code> &lt;?php
   }
   }
   ?&gt;
  </code></pre>