Clicking on suggestion results and filling text box
<p>I have a search box that gives you a list of suggested results based on data from a MySql database. If the user types A and the words <code>Apple</code> and <code>Animal</code> are in the databse, then the suggestion box will show both results. Currently, there is no way for me to click one of the results and have it fill the text box, and no matter what I try and do, I can't seem to get it to work. Currently, I'm working with something that looks like this:</p>

<pre><code>  echo '&lt;script type ="text/javascript"&gt;'; 

  echo 'function textFill(){';
  echo ' var txt=document.getElementById("userInput").value= &lt;li onclick ="textFill(this.value)"&gt;' .$matched[$count].'&lt;/li&gt;'; //Suggestion results
  echo '}';
  echo"&lt;/script&gt;";
 }
</code></pre>

<p>I'll be honest, I'm not sure if this is even the right path to be going, but I found a few examples that were vaguely similar to this, so that's that direction I went with it. With this set up, the suggestion box doesn't even display, but if I take away the <code>echo '&lt;script type ='text/javascript&gt;'</code> it will display the results plus the whole line of <code>getElement</code> code. How would I go about getting this to work properly? I can show more code if necessary. </p>

<p>EDIT:
I'm providing more code to show more of my PHP:</p>

<pre><code>if(isset($_GET['userInput'])){
   $value = $_GET['userInput']; //assign the value
   strtolower($value);
  }else{
   echo "no input";
  }
//Select query

  $sql = "SELECT Device_type FROM Device WHERE Device_type LIKE '%".$value."%';";


  if($result = mysqli_query($conn, $sql)){
   if(mysqli_num_rows($result) &gt; 0){ 
//Store the result in an array list[]

     while($row = mysqli_fetch_array($result)){
     $list[] = $row['Device_type'];
    }
   }else{
//set a null value to list[] if no result to prevent error

    $list[] = "";
   }
  }

  if(!empty($value)){
   if($matched = preg_grep('~'.$value.'~i', $list)){
   $count = 0;
   echo '&lt;ul&gt;';
    while($count &lt; sizeOf($list)){
     if(isset($matched[$count])){

      echo '&lt;li &gt; ' .$matched[$count].'&lt;/li&gt;';

     }
     $count++;
    }
   echo '&lt;/ul&gt;';
   echo json_encode($data);
   }else{
    echo "No result";
   }
</code></pre>