【发布时间】:2016-11-16 00:37:09
【问题描述】:
几天来一直在寻找答案,即使我认为这应该很简单,但我似乎无法让它发挥作用。
首先我用一个while循环从我的数据库中填充一个“选择”。
<select class="admin-content-select" name="sale">
<?php try {
$stmt = $db->prepare('SELECT saleID, saleKal, saleNr FROM wcms_sale');
$stmt->execute(array(':saleKal' => $row['saleKal']));
while($sale = $stmt->fetch()){
echo '<option value="'.$sale['saleKal'].'">'.$sale['saleKal'].'</option>';
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
</select>
然后我有一个输入字段,我需要根据所选内容填充一个值。 The values are on the same row in the db so my initial thought was that it would be easy to say: "when X row is selected, echo Y" but since it's a while loop it will simply echo a lot of input fields.
有什么变通方法还是我在 while 循环中完全错了?
希望我解释得对,并提前感谢您的任何回答:)
答案:
<select id="saleName" class="admin-content-select" onchange="changeFunc()">
<option value="'.$sale['saleNr'].'">'.$sale['saleKal'].'</option>';
然后按照建议添加一个功能:
function changeFunc(){
var x = document.getElementById("selectID").value;
document.getElementById("inputID").value = x;
}
感谢大家的回答!
【问题讨论】:
-
如何知道值是从
wcms_sale中选择的? -
点击相应的
select值时是否需要显示saleKal值? -
我只从该数据库中选择值:“FROM wcms_sale”。或者这不是你的意思?
-
@Arun Sry 我更新了以使其清楚。我需要根据我选择的“saleKal”在输入字段中显示“saleNr”。
-
@christopherlarsen,查看我的更新答案
标签: php mysql loops select while-loop