title: Fill textbox from selected value in dropdown
body: <p>I have a dropdown called <code>product1</code> and a textbox called <code>price1</code>. The dropdown is filled with <code>products</code> from the database. I want to show the <code>price</code> of te selected <code>product</code> in the textbox called 'price1'.</p>
  
  <p>I am using the following table structure:</p>
  
  <pre><code>forms (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `sort` int(1) NOT NULL,
    `name` varchar(100) NOT NULL,
    `prices` decimal(7,2) NOT NULL,
    `tax` int(2) NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
  </code></pre>
  
  <p>With the following code I am trying to fill the textbox with the selected value.</p>
  
  <pre><code>//filename: test.php
  &lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "database";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
       die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  $sql = "SELECT * FROM forms";
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
       echo "&lt;select class='form-control select2' id='product1' name='product1' onChange='getstate(this.value);' style='width: 100%;'&gt;";
       echo "&lt;option selected disabled hidden value=''&gt;&lt;/option&gt;";
       // output data of each row
       while($row = $result-&gt;fetch_assoc()) {
                        echo "&lt;option value='" . $row["id"]. "'&gt;" . $row["name"]. "&lt;/option&gt;";
       }                   
  echo "&lt;/select&gt;";
  } else {
       echo "0 results";
  }
  
  $conn-&gt;close();
  
  ?&gt;
  
  &lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "database";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
       die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  $sql = "SELECT * FROM forms WHERE id='". $product1 ."'";
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
       // output data of each row
       while($row = $result-&gt;fetch_assoc()) {
  
            echo "&lt;div id='price-list'&gt;";
                echo "&lt;input type='text' class='form-control' name='price1' id='price1' value='" . $row["price"]. "'&gt;";
            echo "&lt;/div&gt;";
  
       }                   
  } else {
       echo "0 results";
  }
  
  $conn-&gt;close();
  
  ?&gt;  
  
  &lt;script&gt;
           function getprice(val) {
              $.ajax({
                type: "POST",
                url: "test.php",
                data:'id='+val,
                success: function(data){
                  $("#price-list").html(data);
                }
              });
            }
  &lt;/script&gt;
  
  &lt;?php
  $product1=$_POST['price1'];
  ?&gt;
  </code></pre>
  
  <p>When I select a product in dropdown, nothing is happening.</p>
  
  <p>Can anyone help me to fix this code?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Update 1:</strong> Still not working</p>
  
  <pre><code>//filename: index.php
  
  &lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "database";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
       die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  $sql = "SELECT * FROM forms";
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
       echo "&lt;select class='form-control select2' id='product1' name='product1' onChange='getstate(this.value);' style='width: 100%;'&gt;";
       echo "&lt;option selected disabled hidden value=''&gt;&lt;/option&gt;";
       // output data of each row
       while($row = $result-&gt;fetch_assoc()) {
                        echo "&lt;option value='" . $row["id"]. "'&gt;" . $row["name"]. "&lt;/option&gt;";
       }                   
  echo "&lt;/select&gt;";
  } else {
       echo "0 results";
  }
  
  $conn-&gt;close();
  
  ?&gt;
  &lt;html&gt;
  
  &lt;!-- Your text input --&gt;
  &lt;input id="product_name" type="text"&gt;
  
  &lt;/html&gt;
  
  &lt;script&gt;
  function getPrice() {
  
      // getting the selected id in combo
      var selectedItem = jQuery('.select2 option:selected').val();
  
      // Do an Ajax request to retrieve the product price
      jQuery.ajax({
          url: 'get.php',
          method: 'POST',
          data: 'id=' + selectedItem,
          success: function(response){
              // and put the price in text field
              jQuery('#product_name').val(response);  
          },
          error: function (request, status, error) {
              alert(request.responseText);
          },
      });
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>And</p>
  
  <pre><code>    //filename: get.php
  
  &lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "database";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
       die("Connection failed: " . $conn-&gt;connect_error);
  } 
      $productId = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
  
      $query = 'SELECT price FROM forms WHERE id=' . $productId;
  
      $res = mysql_query($query);
      if (mysql_num_rows($res) &gt; 0) {
          $result = mysql_fecth_assoc($res);
  
          print($result['price']);
  
      die();
  }
  ?&gt;
  </code></pre>