【问题标题】:Ajax multiple dropdown return value- php mysqlAjax 多个下拉列表返回值 - php mysql
【发布时间】:2018-05-22 15:14:04
【问题描述】:

我有 4 个这样的表

Table

然后我要填写这张表格

Form

工作流程是,当我选择Item TypeTreatment时,应该根据ServicePrice Table填写价格,我可以得到ServicePrice 中的 Price,但是当我在网络上查看 XHR 时,它显示了 Price 的值,但它的数据只是第一次显示,我mean when the modal opened, it shows the first data, when the option changed, the Price value keep like the 1st time modal open.所以我希望每当 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() 用于两次更改,因此一旦您更改 ItemType,它将为两者分配相同的 id变量,然后你要改变治疗,这意味着再次改变两个变量的值,这意味着你得到的结果是相同的治疗ID和类型ID,这是完全错误的。
  • 你在 ajax 中得到了什么console.log(result);
  • @Prateik 它说项目类型和治疗以及价格,但如果我更改选项,治疗结果将遵循项目类型值。是的,我的意思是那个东西,它变成了 itemtype 并跟随它。是的,我知道这是我的问题,那么我怎样才能让它变得动态呢?
  • @devsourav 我得到了正确的价格值。但是无法更改所选项目

标签: php jquery mysql ajax autofill


【解决方案1】:

更改事件处理程序中的$(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); 
        }
    });
});

【讨论】:

    猜你喜欢
    • 2011-07-17
    • 2015-05-18
    • 1970-01-01
    • 2020-11-19
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2012-12-17
    相关资源
    最近更新 更多