2013-01-01 114 views
0
$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>"; 

我想設置一個默認值的下拉菜單,但它不能正常工作,請幫助我。不能降設置的默認值下拉菜單

+1

你的選擇字段元素都有名字'custNameColo'&你'$ _GET [ 'defaultCust']'比較..在那裏再次檢查。 – Rikesh

回答

-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>"; 
+0

Downvoter可以在這裏添加評論:) –

+0

它的錯誤。引號不應用於整數比較。永遠。 – 2013-01-01 10:49:10

+0

好吧,@igorpan我想說選項標記中的引號丟失了!這就是爲什麼他沒有得到選擇標籤的默認值! –

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 
} 
?> 
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>