我有三个选择框,用数据库填充数据 首先,他们从数据库中获取数据,但是在编辑时,第二个选择框是从数据库中获取默认值。
以下是主要类别的代码:
<select name="cat" id="cat">
<?php
$res="SELECT * from cat order by catname";
$stmt = $conn->prepare( $res );
$stmt->execute();
while($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value=$cate>$result[catname]</option>";
}
?>
</select>
子类别:
<select name="scid" id="scid">
<?php
if ($row['psubcat']>0) {
$resi="SELECT * from subcat where sid=$row[psubcat]";
$stmt = $conn->prepare( $resi );
$stmt->execute();
while($results = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value=$row[psubcat]>$results[scname]</option>";
}
}
?>
</select>
次级分类:
<select name="sscid" id="sscid">
<?php
if ($sscate>0) {
$resis="SELECT * from ssubcat where sid=$sscate";
$stmt = $conn->prepare( $resis );
$stmt->execute();
while($resultss = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value=$sscate>$resultss[sscname]</option>";
}
}
?>
</select>
</div>
使用ajax根据密钥更改动态更改类别。
答案 0 :(得分:0)
您需要查询记录哪些是主类别的子类别,但是选择存储的db值子类别。 子类别:
<select name="scid" id="scid">
<?php
if ($row['psubcat']>0)
{
$resi="SELECT * from subcat where cat_id=$row['cat_id']";
$stmt = $conn->prepare( $resi );
$stmt->execute();
while($results = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo '<option value="'.$results[psubcat].'" '.($row[psubcat] == $results[psubcat] ? 'selected' : '').'>'.$results[scname].'</option>';
}
}
?>
</select>