title: Auto complete base on value of dropdown in database
body: <p>I'm stuck with this problem with in a week. What I have here is dropdown select with ajax posting value to another dropdown but now I need to post into textbox with autocomplete function. What I need is connect my autocomplete query and my ajax so that if I select for example ballpen, all ballpen will recommend in autocomplete. Please Help me with this. I need to finish it.</p>
  
  <p>Here's my code</p>
  
  <p><strong>Ajax.php</strong></p>
  
  <pre><code>&lt;script&gt;
  $(document).ready(function(){
   $("#tag").autocomplete("autocomplete.php", {
          selectFirst: true
      });
  });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;br/&gt;
   Drop1
   &lt;?php
      $mysqli = new mysqli("localhost", "root", "", "2015");
      $combo = $mysqli-&gt;query("SELECT * FROM category GROUP BY cat_code ORDER BY id");
      $option = '';
       while($row = $combo-&gt;fetch_assoc())
          {
          $option .= '&lt;option value = "'.$row['cat_code'].'"&gt;'.$row['category'].'&lt;/option&gt;';
          }
      ?&gt;
  
  &lt;select id="main" name="main"&gt;
  &lt;option value="" disabled="disabled" selected="selected"&gt;Choose&lt;/option&gt;
  &lt;?php echo $option; ?&gt;
  &lt;/select&gt;
  Auto Complete &lt;input id="tag"&gt;
  &lt;script type="text/javascript"&gt;
  $('#main').change(function(){
  $.ajax({
  url : 'getajax.php',
  data :{mainlist_id : $(this).val()},
  dataType:'html',
  type:'POST',
  success:function(data){
  $('#tag').html(data);
  }
  });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>getajax.php</p>
  
  <p>In here I post the value in another dropdown but not I need to post into textbox.</p>
  
  <pre><code>&lt;?php
  if (isset($_POST["mainlist_id"])) {
      $mysqli = new mysqli("localhost", "root", "", "2015");
      $main = $mysqli-&gt;real_escape_string($_POST["mainlist_id"]);
  
  
  $result1 = $mysqli-&gt;query("SELECT * FROM code WHERE cat_code='$main' GROUP BY item_code ORDER BY item");
  
      while($row = $result1-&gt;fetch_assoc())
      {
      ?&gt;
      &lt;option value ="&lt;?php echo $row['item_code'];?&gt;"&gt;&lt;?php echo $row['item'];?&gt;&lt;/option&gt;';
  &lt;?php
      }
      }
  ?&gt;
  </code></pre>
  
  <p>autocomplete.php</p>
  
  <pre><code>&lt;?php
      //$q=$_GET['q'];
      $mysqli = new mysqli("localhost", "root", "", "2015") or die("Database Error");
      $auto = $mysqli-&gt;real_escape_string($_GET["q"]);
      //$main = $mysqli-&gt;real_escape_string($_POST["mainlist_id"]); AND cat_code='$main'
      $sql = $mysqli-&gt;query("SELECT * FROM code WHERE item LIKE '%$auto%' GROUP BY id ORDER BY item" ); 
  
      if($sql)
      {
          while($row=mysqli_fetch_array($sql))
          {
              echo $row['item']."\n";
          }
      }
  ?&gt;
  </code></pre>