【发布时间】:2015-08-11 09:38:05
【问题描述】:
我正在尝试使用下拉菜单将我的数据库值显示到文本框中。这是我做的,它正在显示。这里的问题是,当我在下拉列表中选择一个项目时,它会回到第一个选择或最后一个选择,我得到的解释是,我的循环正在选择字段中的所有项目,导致下拉菜单当我点击其他项目时回到第一个选择。当我选择其他选项时,您能帮我提供有关如何停止回到第一选择的代码吗?这是我的整个代码。我也使用函数。
home.php
<?php
session_start();
include('dbconnect.php');
include('functions.php');
if(isset($_POST['brandname'])){
$id = $_POST['brandname'];
$result = mysql_query("SELECT * FROM tblstore WHERE brandname = '$id'");
while($row = mysql_fetch_array($result)){
$price = $row['price'];
$stocks = $row['stocks'];
}
}
?>
<html>
<body>
<form method="POST" name="">
<table align="center">
<tr>
<td>Choose here:</td>
<td>
<select name = "brandname" onchange = "this.form.submit()">
<?php dropdown() ?>
</select>
</td>
</tr>
<tr>
<td>Quantity:</td>
<td><input type="text" name="qty" id="qty" value="" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" id="price" value="<?php echo $price ?>" disabled/></td>
</tr>
<tr>
<td>Stocks:</td>
<td><input type="text" name="stocks" id="stocks" value="<?php echo $stocks ?>" disabled/></td>
</tr>
<tr>
<td>Total:</td>
<td><input type="text" name="total" id="total" disabled/></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
<div align = "center">
hi' <?php echo $userRow['username']; ?> <a href="logout.php?logout">Sign Out</a>
</div>
</body>
</html>
functions.php
<?php
function dropdown(){
$all = mysql_query("SELECT * FROM tblstore");
while($row = mysql_fetch_array($all)){
echo "<option value = '".$row['brandname']."' selected='selected'>" .$row['brandname'] . "</option>";
}
}
随意编辑整个代码。我是 php 的初学者,正在学习我的方法。谢谢
【问题讨论】:
-
你的循环中需要 selected='selected' 吗?只是意味着每一个都会被选中
-
是..如果我删除选择标签..在我选择任何选项后,下拉菜单上的第一个选择总是被选中..但是如果我包含选择标签..最后一个选择在我选择其他选项后,总是会选择下拉菜单的 .. 是的.. 每个选项都会被选中,这正是我的问题.. :D