如何在下面的代码中添加所选内容?
echo '<select class="w-select selection-produit" id="field" name="Ville">';
echo '<option value="">Choisir une ville...</option>';
$req19="SELECT * FROM ville";
$res19=mysql_query($req19);
while($data4=mysql_fetch_array($res19)){
echo '<option value="'.$data4['Nom_ville'].'">'.$data4['Nom_ville'].'</option>';
}
echo '</select>';
答案 0 :(得分:0)
正如评论中所指出的,只需使用selected
即可。例如。以下代码检查该镇是否为Mulhouse
(oùestlemuséeduchemin de fer ...),如果是,则选择此条目。
echo '<select class="w-select selection-produit" id="field" name="Ville">';
echo '<option value="">Choisir une ville...</option>';
$req19="SELECT * FROM ville";
$res19=mysql_query($req19);
while($data4=mysql_fetch_array($res19)) {
// check if the town is equal to Mulhouse
// equal to if (this) else (that)
$sel = ($data4['Nom_ville'] == 'Mulhouse')?"selected":"";
echo "<option value='".$data4['Nom_ville']."' $sel>".$data4['Nom_ville']."</option>";
}
echo '</select>';
答案 1 :(得分:-1)
$valueWantSelect="the value you want selected";
$define = "";
echo '<select class="w-select selection-produit" id="field" name="Ville">';
echo '<option value="">Choisir une ville...</option>';
$req19="SELECT * FROM ville";
$res19=mysql_query($req19);
while($data4=mysql_fetch_array($res19))
{
$define = ($data4['Nom_ville'] == $valueWantSelect) ? "selected='selected'" : "";
echo '<option value="'.$data4['Nom_ville'].'" '.$define.'>'.$data4['Nom_ville'].'</option>';
$define="";
}
echo '</select>';
答案 2 :(得分:-1)
尝试这样希望它适合你
foreach ($request as $data) {
if ($data->cat_id == $catee) {
echo "<option selected = 'selected' value='" . $data->cat_id . "'>" . $data->cat_name . "</option>";
} else {
echo "<option value='" . $data->cat_id . "'>" . $data->cat_name . "</option>";
}
}