我有一个使用 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>
关于php - 根据选择框选项用 MySQL 值填充其他文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368020/