title: PHP update textbox when value of dropdown is changed
body: <p>I filled my dropdown menu with data from the database. I can select here the name of the product. I have a second textbox on my page. Here i want to show the price of the selected product. I tried different ways but I o net get it working.</p>
  
  <p>First of all this is my dropdown menu where the product name is showed:</p>
  
  <pre><code>    &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 form";
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
       echo "&lt;select class='form-control select2' id='product1' name='product1' 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 onchange='OnChange()' value='" . $row["id"]. "'&gt;" . $row["name"]. "&lt;/option&gt;";
       }                   
  echo "&lt;/select&gt;";
  } else {
       echo "0 results";
  }
  
  $conn-&gt;close();
  
  ?&gt;
  </code></pre>
  
  <p>I want to show the price of the selected value in this textbox:</p>
  
  <pre><code>&lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "dbsi";
  
  // 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 price FROM form 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;input type='text' class='form-control' name='price1' id='price1' onkeyup='getValues()' value='" . $row["price1"]. "'&gt;";
  
  
       }                   
  } else {
       echo "0 results";
  }
  
  $conn-&gt;close();
  
  ?&gt;  
  </code></pre>
  
  <p>Using this script i only get "0 results" in price1. It doesnt update when i change the selected value of the dropdown menu. How can I ensure that price1 gets updated when i select another value in the dropdown menu?</p>
  
  <p><strong>Update 1:</strong>
  I tried to add the the following script</p>
  
  <pre><code>&lt;script&gt;
  function OnChange(){
    UpdatePoints(&lt;?php echo $price1; ?&gt;);
  }
  echo "&lt;script type='text/javascript'&gt; window.onchange=load; &lt;/script&gt;";
  &lt;/script&gt;
  </code></pre>
  
  <p>When I use this script i still get "0 results"</p>
  
  <p><strong>Update 2:</strong>
  The following is still not working:</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>