John
John

Reputation: 952

PHP update textbox when value of dropdown is changed

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.

First of all this is my dropdown menu where the product name is showed:

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM form";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<select class='form-control select2' id='product1' name='product1' style='width: 100%;'>";
     echo "<option selected disabled hidden value=''></option>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
                      echo "<option onchange='OnChange()' value='" . $row["id"]. "'>" . $row["name"]. "</option>";
     }                   
echo "</select>";
} else {
     echo "0 results";
}

$conn->close();

?>

I want to show the price of the selected value in this textbox:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbsi";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT price FROM form WHERE id='". $product1 ."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {

              echo "<input type='text' class='form-control' name='price1' id='price1' onkeyup='getValues()' value='" . $row["price1"]. "'>";


     }                   
} else {
     echo "0 results";
}

$conn->close();

?>  

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?

Update 1: I tried to add the the following script

<script>
function OnChange(){
  UpdatePoints(<?php echo $price1; ?>);
}
echo "<script type='text/javascript'> window.onchange=load; </script>";
</script>

When I use this script i still get "0 results"

Update 2: The following is still not working:

//filename: test.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM forms";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<select class='form-control select2' id='product1' name='product1' onChange='getstate(this.value);' style='width: 100%;'>";
     echo "<option selected disabled hidden value=''></option>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
                      echo "<option value='" . $row["id"]. "'>" . $row["name"]. "</option>";
     }                   
echo "</select>";
} else {
     echo "0 results";
}

$conn->close();

?>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM forms WHERE id='". $product1 ."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {

          echo "<div id='price-list'>";
              echo "<input type='text' class='form-control' name='price1' id='price1' value='" . $row["price"]. "'>";
          echo "</div>";

     }                   
} else {
     echo "0 results";
}

$conn->close();

?>  

<script>
         function getprice(val) {
            $.ajax({
              type: "POST",
              url: "test.php",
              data:'id='+val,
              success: function(data){
                $("#price-list").html(data);
              }
            });
          }
</script>

<?php
$product1=$_POST['price1'];
?>

Upvotes: 1

Views: 3896

Answers (1)

Arun Kumaresh
Arun Kumaresh

Reputation: 6311

make a ajax call and get the value of using post   

 <script>
         function getprice(val) {
            $.ajax({
              type: "POST",
              url: "index.php",
              data:'priceid='+val,
              success: function(data){
                $("#price-list").html(data);
              }
            });
          }
    </script>

    <select class='form-control select2' id='product1' name='product1' style='width: 100%;'  onChange="getstate(this.value);">
    //yourcode
    </select>

    <div id="price-list">
    </div>



index.php
<?php
$product1=$_POST['priceid'];
?>

Upvotes: 1

Related Questions