当更改下拉列表的值时,PHP更新文本框

时间:2016-02-19 04:31:09

标签: php mysql ajax

我在下拉菜单中填充了数据库中的数据。我可以在这里选择产品的名称。我的页面上有第二个文本框。在这里,我想显示所选产品的价格。我尝试了不同的方法,但我网络让它发挥作用。

首先,这是我的下拉菜单,其中显示了产品名称:

    <?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();

?>

我想在此文本框中显示所选值的价格:

<?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();

?>  

使用此脚本我只在price1中获得“0结果”。当我更改下拉菜单的选定值时它不会更新。当我在下拉菜单中选择另一个值时,如何确保price1更新?

更新1: 我尝试添加以下脚本

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

当我使用这个脚本时,我仍然得到“0结果”

更新2: 以下内容仍无效:

//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'];
?>

1 个答案:

答案 0 :(得分: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'];
?>