1

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'];
?>
11
  • where'd you get $row["price1"] index, you didn't select a column with that name, maybe you mean just $row["price"], or is this just a typo of some sort Commented Feb 19, 2016 at 4:32
  • and just make sure the php page has a proper form with submit buttons and stuff, and $product1 is undefined in the second code block Commented Feb 19, 2016 at 4:35
  • You can use ajax or javascript on_load() to automatically load the value into the textbox when the dropdown select change. Commented Feb 19, 2016 at 4:38
  • I use $row["price1"] for calculations. The calculations are working fine. Commented Feb 19, 2016 at 4:39
  • 1
    You have to use ajax and jquery onchange get the value from drop down and with that value select from the price Commented Feb 19, 2016 at 4:54

1 Answer 1

1
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'];
?>
Sign up to request clarification or add additional context in comments.

12 Comments

Still get "0 results". See Update 2.
what you have tired post the entire code where did you make ajax call
@John : pls make sure $sql = "SELECT * FROM forms WHERE id='". $product1 ."'"; is creating correct And also do not forgot to concat the price1 field if there are more than one
When I send $product1 to the database it sends the ID. The query SELECT * FROM forms WHERE id = '1' is working. To be sure I tested it like SELECT * FROM forms WHERE name='. $product1 .' but the result is the same @sinto
This is the Ajax call, right? <script> function getprice(val) { $.ajax({ type: "POST", url: "test.php", data:'id='+val, success: function(data){ $("#price-list").html(data); } }); } </script> This is at the bottom of the page @ArunKumaresh
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.