无法在下拉菜单中设置默认值

时间:2013-01-01 10:14:17

标签: php drop-down-menu

$sql = "select custName from customer where active = 1 order by custName";
sult=mysql_query($sql);
echo"<select name='custNameColo'>";
while($row = mysql_fetch_array($result)) {
    if($row['custName']==$_GET['defaultCust']) {
        echo "<option value=".$row['custName']."selected = 'selected'>".$row['custName']."</option>";
    }
    else {
        echo "<option value=".$row['custName'].">".$row['custName']."</option>";
    }
}
echo "</select>";

我想在下拉菜单中设置默认值,但它不起作用,请帮助我。

3 个答案:

答案 0 :(得分:0)

<?php 
$sql = "select custName from customer where active = 1 order by custName";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result)){
?>
<option value="<?php echo $row['custname'] ?>" <?php if($row['custname']==$_GET['defaultCust']) { echo "selected";}?>>
<?php echo $row['custname']; ?></option>
<?php
}
 ?>

答案 1 :(得分:0)

<?  echo "TRY this it will work";
$sql = "select custName from customer where active = 1 order by custName";
$result=mysql_query($sql);
?>
<select name="custNameColo">
<? 
while($row = mysql_fetch_array($result)){?>
<option value="<?=$row['custname'];?>" <? if($row['custname']==$_GET['defaultCust']){?> selected="selected" <? }?>><?=$row['custname'];?></option>
<? } ?>
</select>

答案 2 :(得分:-1)

您之前和之后都没有在选项标记的值中添加单引号。我为你做了。未选择默认值,因为未正确关闭值属性。

$sql = "select custName from customer where active = 1 order by custName";
$result=mysql_query($sql);
echo "<select name='custNameColo'>";
while($row = mysql_fetch_array($result)){
    if($row['custName']==$_GET['defaultCust']){
        echo "<option value='".$row['custName']."' selected = 'selected'>".$row['custName']."</option>";
    }
    else {
        echo "<option value='".$row['custName']."'>".$row['custName']."</option>";
    }
}
echo "</select>";