title: Getting data from a php file using Javascript and Ajax
body: <p>Hi I am trying to create a Survey for a personal project that I am working    on to teach my self web development. The survey is for an F1 website and I want to be able to ask the users five questions and based on there answers I want to be able to tell them what Grand Prix is best suited to them. </p>
  
  <p>My questions for the users are: </p>
  
  <ol>
  <li><p>what grand Prix’s are closest to you?  (I have given them the option
  to select 3  Grand Prix’s by suing select boxes)</p></li>
  <li><p>What countries would you like to visit? (I have given them the
  option to select 2  Countries by suing select boxes)</p></li>
  <li><p>When are you available to go to a Grand Prix (I have given them the
  option to select 3 months by suing select boxes)</p></li>
  <li><p>What is your favorite team? (one option using a select box)</p></li>
  <li><p>Who is your favorite driver? (One  option using select box)</p></li>
  </ol>
  
  <p>So far I have created a database with tables for countries, Grand Prix’s, months, teams and drives.</p>
  
  <p>I have a Survey.php file that populates the select boxes from the database.</p>
  
  <p>So my question is how do I create a JavaScript file link it to the php file so it populates the select boxes to get the input from the user and display the result with out refreshing the page using Ajax?</p>
  
  <pre><code>My Survey.php 
  
  
      &lt;?php
  
      $link = mysqli_connect("localhost","student","student");
      mysqli_select_db($link,"gpdb");
  
  
      //question 1
      $query = "SELECT * from gp";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result);
      ?&gt;
  
      &lt;div class="Circuits"&gt;
      &lt;h1&gt;Which Grand Prix's are closest to you&lt;h1&gt;
      &lt;form action="&lt;?php $_SERVER['PHP_SELF']; ?&gt;" method="get" &gt;
      &lt;p&gt;Option 1&lt;/p&gt;
      &lt;select name="GrandPrix"&gt;
      &lt;?php do { ?&gt;    
                  &lt;option value="&lt;?php echo $row['Circuits'];  ?&gt;"&gt;&lt;?php echo                        
      $row['Circuits'];  ?&gt;&lt;/option&gt;
              &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
           &lt;/select&gt;
  
  
      &lt;?php
      $query = "SELECT * from gp";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result);
      ?&gt;
  
  
      &lt;p&gt;Option 2&lt;/p&gt;
      &lt;select name="GrandPrix2"&gt;
      &lt;?php do { ?&gt;    
                  &lt;option value="&lt;?php echo $row['Circuits'];  ?&gt;"&gt;&lt;?php echo           
      $row['Circuits'];  ?&gt;&lt;/option&gt;
          &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
      &lt;/select&gt;
  
      &lt;?php
      $query = "SELECT * from gp";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result);
      ?&gt; 
  
      &lt;p&gt;Option 3&lt;/p&gt;
      &lt;select name="GrandPrix3"&gt;
      &lt;?php do { ?&gt;    
                  &lt;option value="&lt;?php echo $row['Circuits'];  ?&gt;"&gt;&lt;?php echo         
      $row['Circuits'];  ?&gt;&lt;/option&gt;
          &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
      &lt;/select&gt;&lt;br /&gt;
      &lt;/form&gt;
      &lt;/div&gt;
  
      &lt;?php
      $query = "SELECT * from place";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result); 
      ?&gt;
  
      &lt;div class="Country"
      &lt;h1&gt;What countries would you like to visit?&lt;/h1&gt;
      &lt;p&gt;Country 1&lt;/p&gt;
      &lt;select name="Country1"&gt;
      &lt;?php do { ?&gt;
          &lt;option value="&lt;?php echo $row['Location']; ?&gt;"&gt;&lt;?php echo     
      $row['Location']; ?&gt;&lt;/option&gt;
              &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
        &lt;/select&gt;
  
      &lt;?php
      $query = "SELECT * from place";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result); 
      ?&gt;
      &lt;p&gt;Country 2&lt;/p&gt;
      &lt;select name="Country1"&gt;
      &lt;?php do { ?&gt;
          &lt;option value="&lt;?php echo $row['Location']; ?&gt;"&gt;&lt;?php echo     
      $row['Location']; ?&gt;&lt;/option&gt;
              &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
        &lt;/select&gt;
      &lt;/div&gt;
  
  
  
      &lt;?php
      $query = "SELECT * from month";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result);
      ?&gt;
  
      &lt;div class="month"&gt;
      &lt;h1&gt;When are you available to go to a grand prix&lt;/h1&gt;
       &lt;p&gt;Month 1&lt;/p&gt;
      &lt;select name="Month"&gt;
      &lt;?php do { ?&gt;    
                  &lt;option value="&lt;?php echo $row['monthofgp'];  ?&gt;"&gt;&lt;?php echo     
        $row['Location']; ?&gt;&lt;/option&gt;
          &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
      &lt;/select&gt;
  
      &lt;?php
      $query = "SELECT * from month";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result);
      ?&gt;
      &lt;p&gt;Month 2&lt;/p&gt;
      &lt;select name="Month"&gt;
      &lt;?php do { ?&gt;    
                  &lt;option value="&lt;?php echo $row['monthofgp'];  ?&gt;"&gt;&lt;?php echo     
      $row['monthofgp'];  ?&gt;&lt;/option&gt;
          &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
      &lt;/select&gt;
  
      &lt;?php
      $query = "SELECT * from month";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result);
      ?&gt;
      &lt;p&gt;Month 3&lt;/p&gt;
      &lt;select name="Month"&gt;
      &lt;?php do { ?&gt;    
                  &lt;option value="&lt;?php echo $row['monthofgp'];  ?&gt;"&gt;&lt;?php echo 
     $row['monthofgp'];  ?&gt;&lt;/option&gt;
          &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
      &lt;/select&gt;&lt;br /&gt;
       &lt;/div&gt;
  
  
      &lt;div class="Driver"&gt;   
      &lt;?php
      $query = "SELECT * from teamName";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result);
      ?&gt;
  
      &lt;h1&gt;What is favourite team?&lt;/h1&gt;
  
       &lt;p&gt;Team&lt;/p&gt;
      &lt;select name="taem"&gt;
      &lt;?php do { ?&gt;    
                  &lt;option value="&lt;?php echo $row['Team'];  ?&gt;"&gt;&lt;?php echo 
      $row['Team'];  ?&gt;&lt;/option&gt;
          &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
      &lt;/select&gt;&lt;br /&gt;
      &lt;/div&gt;
  
  
  
      &lt;div class="Driver"&gt;
      &lt;?php
      $query = "SELECT * from driverName";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_assoc($result);
      ?&gt;
  
      &lt;h1&gt;Who is your favourite driver?&lt;/h1&gt;
  
       &lt;p&gt;Driver&lt;/p&gt;
       &lt;select name="taem"&gt;
      &lt;?php do { ?&gt;    
                  &lt;option value="&lt;?php echo $row['Driver'];  ?&gt;"&gt;&lt;?php echo    
      $row['Driver'];  ?&gt;&lt;/option&gt; 
          &lt;?php } while ($row = mysqli_fetch_assoc($result)); ?&gt;
          &lt;/select&gt;&lt;br /&gt;
      &lt;/div&gt;
  
  
      &lt;input type="submit" /&gt;
      &lt;input type="reset" /&gt;
  
      &lt;/form&gt; 
  </code></pre>