这是我的下拉列表代码
<?php
$sql = "SELECT * FROM parts";
$result = mysql_query($sql);
echo "<select name='name' class='ed'>";
echo "<option id='0'> --Select Part Name-- </option>";
while ($row = mysql_fetch_array($result)) {
$op1=$row['part_description'];
$op2=$op1.$row['price'];
$op3=$op2.$row['weight'];
echo "<option value='" . $row['part_description'] ."'>" .$op3 ."</option>";
echo $op3;
}
echo "</select>";
?>
我的数据库有3列partdescription,价格和重量。在我创建的下拉列表中,您可以在part_description中选择存储的项目。我想做的是,如果我在下拉列表中选择该项目,它还会显示价格和重量。但是显示器应该是开箱即用的。
请帮我谢谢
<?php
$sql = "SELECT part_description FROM parts";
$result = mysql_query($sql);
echo "<select name='name' class='ed'>";
echo "<option id='0'> --Select Part Name--</option>";
while ($row = mysql_fetch_array($result)) {
$op1=$row['part_description'];
$op2=$op1.$row['price'];
$op3=$op2.$row['weight'];
echo "<option value='" . $row['part_description'] ."'>" .$op3 ."</option>";
}
echo "</select>";
?>
这是用于在单选标记中打印三个值。让我知道这是你的期望吗。
您可以在Javascript中完成。
<?php
$parts = mysqli_query($conn, "SELECT part_description, price, weight FROM parts");
$theParts = [];
while ($row = mysqli_fetch_assoc($parts)) {
$part = [];
$part['description'] = $row['part_description'];
$part['price'] = $row['price'];
$part['weight'] = $row['weight'];
$theParts[] = $part;
}
?>
<script type="text/javascript">
var theParts = <?php echo(json_encode($theParts));?>;
function createDropDown() {
var theDropdown = "<select name='name' id='theDropDown' class='ed' onchange='didChange()'>";
theDropdown += "<option value='0'>--Select Part Name--</option>";
theParts.forEach(function(entry) {
theDropdown += "<option value='" + entry['description'] + "'>" + entry['description'] + "</option>";
});
theDropdown += "</select>";
return theDropdown;
}
function didChange() {
var e = document.getElementById("theDropDown");
var str = e.options[e.selectedIndex].value;
var price = null;
var weight = null;
theParts.forEach(function(entry) {
if (entry['description']==str) {
price = entry['price'];
weight = entry['weight'];
}
});
}
</script>
(我直接在StackOverflow中输入了这段代码,还没有测试过,但它应该可以工作)
编辑:以下是如何使用MySQL,尽管我不建议使用它。
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database', $conn);
$results = mysql_query("SELECT part_description, price, weight FROM parts");
while ($row = mysql_fetch_assoc($results)) {
//Same as above.
}