title: Filling form values from database based on the selected value from dropdown
body: <p>I've a form like this and if the user selected any value from the dropdown then I want to fetch the details from the database based on that value. Suppose say if the user selected <strong>Name 2</strong> from the dropdown then I should automatically fill the fields <strong>address &amp; phone</strong> that are relevant to <strong>Name 2</strong>. Also I'm unable to get the value <strong>$_POST['name']</strong>.
  I've tried but no luck. Here is my code. Please suggest me if is there any other way to achieve it. Thank you.</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;select id="name" name="name"&gt;
      &lt;option value="1"&gt;Name 1&lt;/option&gt;
      &lt;option value="2"&gt;Name 2&lt;/option&gt;
      &lt;option value="3"&gt;Name 3&lt;/option&gt;
      &lt;option value="4"&gt;Name 4&lt;/option&gt;
      &lt;option value="5"&gt;Name 5&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;input type="text" name="address" id="address" /&gt;
  &lt;input type="text" name="phone" id="phone" /&gt;
  </code></pre>
  
  <p>jQuery</p>
  
  <pre><code>$(document).ready(function () {
      $('#name').change(function(){
          var name = $(this).val();
          var dataString = "name=" + name;
          alert (dataString);
          $.ajax ({
              type: "POST",
              url: "test.php",
              data: dataString,
              dataType: json,
              success: function(data) {
                  $('#address').val(data.address);
                  $('#phone').val(data.phone);
              }
          });
      });
  });
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  
  ini_set('display_errors',1); // enable php error display for easy trouble shooting
  error_reporting(E_ALL); // set error display to all
  
  include "connect.php";
  
  if (ISSET($_POST['name'])) {
  
      $ref = $_POST['name'];
  
      $query = $con-&gt;query("SELECT * FROM test WHERE id = '$ref' LIMIT 1");
      $row = $query-&gt;fetch_assoc();
  
      $address = $row['address'];
      $phone = $row['phone'];
  
      $json = array('address' =&gt; $address, 'phone' =&gt; $phone);
      echo json_encode($json);
  }
  
  $con-&gt;close();
  ?&gt;
  </code></pre>