[英]Fill inputs that are generated dynamically with jquery while selecting an option in the select box
[英]fill other text inputs with MySQL values based on select box option
我有這個選擇框,使用PHP從MySQL數據填充
<select>
<?php
$stmt = $pdo_conn->prepare("SELECT * from prices ");
$stmt->execute(array());
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($records as $result2) {
echo '<option value="'.$result2["product"].'">'.$result2["product"].'</option>';
}
?>
</select>
在MySQL(價格)的同一個表中有一個零售專欄,我怎樣才能將零售價值放入文本輸入中
<input type="text" name="unitprice" />
基於選擇框中的選定行?
我建議使用JavaScript。
選項1
我會將您的結果轉換為JSON數組並將其回顯給JavaScript。 這樣,所有信息都會被用戶提供。
<select id="mySelectBox" onchange="changeValue(this.value);">
<?php
$json_array = array();
$stmt = $pdo_conn->prepare("SELECT * from prices ");
$stmt->execute(array());
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($records as $result2) {
$product = $result2["product"];
$json_array[$product] = $result2['unitprice'];
echo '<option value="'.$result2["product"].'">'.$result2["product"].'</option>';
}
json_encode($json_array, JSON_FORCE_OBJECT);
?>
</select>
<input type="text" id="unitPrice" name="unitprice" />
<script>
var json = <?php echo $json_array; ?>;
function changeValue(myValue) {
document.getElementById("unitPrice").value = json[myValue];
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.