php - Ajax 多下拉返回值- php mysql

标签 php jquery mysql ajax autofill

我有4张这样的 table

Table

然后我要填写这个表格

Form

工作流程是,当我选择Item TypeTreatment时,价格应该根据ServicePrice Table填写,我可以得到ServicePrice 中的 Price,但是当我在网络上检查 XHR 时,它显示了 Price 的值,但它的数据只是第一次显示,我意味着当模态打开时,它显示第一个数据,当选项更改时,价格 值保持像第一次模态打开一样。所以我想每当 Item TypeTreatment 改变时,我想让价格框动态填充

这是我的表格

<tr>
    <td>Item Type</td>
    <td>
        <select name="ItemTypeID"  class="form-control"  id="ItemType" >
            <option selected=""></option>
            <?php 
            $sql2 = mysql_query("select * from itemtype");
            while($record = mysql_fetch_array($sql2)){
            ?>
                <option value="<?php echo $record['ID']; ?>" title="<?php echo $record['DefaultSpec']; ?>"> <?php echo $record["Name"]; ?> </option>
            <?php } ?>
        </select>
    </td>
</tr>

<tr>
    <td>Treatment</td>
    <td>
        <select name="TreatmentID" class="form-control" id="Treatment">
            <?php 
            $sql2 = mysql_query("select * from treatment");
            while($record = mysql_fetch_array($sql2)){
            ?>
                <option value="<?php echo $record['ID']; ?>"> <?php echo $record["Name"]; ?> </option>
            <?php } ?>
        </select>
    </td>
</tr>

然后是我的ajax

$("#ItemType, #Treatment").change(function(){ 
    var ItemType = $(this).val();
    var Treatment = $(this).val(); 
    console.log(Treatment);
    console.log(ItemType);
    $.ajax({ 
        type: "POST", 
        dataType: "html",
        url: "GetPrice.php", 
        data: {ItemTypeID: ItemType, TreatmentID: Treatment}, 
        success: function(result){ 
        console.log(result);
        $("#Price").val(result); 
    });
});

我的 GetPrice.php

<?php include "../Content/connection.php"; 

$a="SELECT * FROM ServicePrice WHERE ItemtypeID = '".$_POST["ItemTypeID"]."' AND TreatmentID = '".$_POST["TreatmentID"]."'";
$q=mysql_query($a);
while($record=mysql_fetch_array($q)){
    echo $record['Price'];
}
?>

编辑:

我是这样做的,它给了我正确的答案,但价格值只有在治疗下拉菜单发生变化时才会触发,我怎样才能让它通过展位下拉菜单触发?

 $("#ItemType").change(function(){ 
  var ItemType = $(this).val(); 
$("#Treatment").change(function(){ 
  var Treatment = $(this).val(); 

最佳答案

更改事件处理程序中的 $(this).val() 将无法获取两个字段的数据, 而是分别获取 ItemTypeTreatment 的数据

$("#ItemType, #Treatment").on('change', function(){
    var ItemType = $("#ItemType").val();
    var Treatment = $("#Treatment").val(); 

    $.ajax({ 
        type: "POST",
        url: "GetPrice.php", 
        data: {ItemTypeID: ItemType, TreatmentID: Treatment}, 
        success: function(result){ 
            $("#Price").val(result); 
        }
    });
});

关于php - Ajax 多下拉返回值- php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47713262/

相关文章:

javascript - 每 18 小时循环倒数计时器,并具有特定的开始日期

javascript - 按值对数组元素排序

javascript - 单击时菜单不切换(关闭)

mysqldump导致数据库连接错误?

mysql - 我们可以使用 Nodejs ORM2 从 MySql 获取表模型而不定义所有属性/字段吗?

php - MySQL带条件select查询排名列表

javascript - 更新名称后,我的数据库 `first_name` 显示为 0,last_name 显示为 "Arora"

php - 使用 Google API 客户端生成器生成 Cloud Endpoints PHP 客户端时出现空目录

javascript - 以 rgb() 格式获取颜色的 r、g、b 分量

MySQL 查询带回那些不在列表中的