Ajax 多个下拉列表 return 值 - php mysql
Ajax multiple dropdown return value- php mysql
我有 4 个 table 这样的
Table
然后我要填写这张表格
Form
工作流程是,当我select 项目类型和治疗时,价格应该根据ServicePrice Table,我可以从 ServicePrice 得到 Price,但是当我检查 XHR 时network, it shows the value of Price but its data just show for 1st time, I mean when the modal opened, it shows the first data, when the option changed, the Price 值保持像第一次模式打开一样。所以我想每当 Item Type 或 Treatment 改变时,我想让价格框动态填充
这是我的表格
<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 和 Treatment 的数据
$("#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);
}
});
});
我有 4 个 table 这样的
Table
然后我要填写这张表格
Form
工作流程是,当我select 项目类型和治疗时,价格应该根据ServicePrice Table,我可以从 ServicePrice 得到 Price,但是当我检查 XHR 时network, it shows the value of Price but its data just show for 1st time, I mean when the modal opened, it shows the first data, when the option changed, the Price 值保持像第一次模式打开一样。所以我想每当 Item Type 或 Treatment 改变时,我想让价格框动态填充
这是我的表格
<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 和 Treatment 的数据
$("#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);
}
});
});