php combobox默认选中

时间:2012-07-21 00:26:25

标签: php mysql combobox default

想象一下:

可以是YES或NO的字段 - >主动:是|有效:否

所以,我得到一个查询,为每个客户端返回它。

我想在表单中显示结果,我应该能够编辑它并再次保存在BD上。

我几乎可以做到,但是我需要默认值已经保存在BD上,而我的atm似乎显示了最后一行或其他内容。

我想要显示当前状态或能够将其更改为(是或否)

组合框就是这样:

$sql = "SELECT * FROM client,sector WHERE client.idClient = '$id' AND sector.idSector=client.Sector_idSector ";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{

<select name="alterar_sector" id="sectorbox" >
    <?php 
    while($row = mysql_fetch_array($result))
    {
    echo '<option value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
    }

    ?>
    </select>

}

不要重视显示“扇区”甚至查询等信息。

恢复:

我希望组合框能够将当前值显示为默认值,并列出要更改的其他可能值。

谢谢:)

2 个答案:

答案 0 :(得分:5)

我可能错过了问题的重点,但是你是否在问如何让一个字段显示为被选中? 简单修改这一行:

echo '<option value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';

拥有:

echo '<option selected="selected" value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';

如果这是您想要选择的特定选项...

您可能会遇到以下情况:

while($row = mysql_fetch_array($result))
{
  if ($sectorActive != $row['idSector']) {
    echo '<option value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
  }else{
    echo '<option selected="selected" value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
  }
}

好的,所以如果你想让一个选择字段有一个默认值,你需要selected="selected"作为<option>元素中的参数值。按照你的例子:

<select name="gender">
  <option>Male</option>
  <option>Female</option>
  <option selected="selected">Other</option>
  <option>Not Specified</option>
</select>

因此,如果您有查询提取记录,并且您希望确保选择了正确的值。您需要有一个随每条记录而变化的变量 - 您需要在该HTML插入中安装一些逻辑,如下所示:(此示例将输出与上述示例相同的HTML ...)

<?
    $usersGender = 'Other'; // This value should be set via the loop, and from a database value.
?>
<select name="gender">
  <option <?=($usersGender=='Male')?'selected="selected"':''?>>Male</option>
  <option <?=($usersGender=='Female')?'selected="selected"':''?>>Female</option>
  <option <?=($usersGender=='Other')?'selected="selected"':''?>>Other</option>
  <option <?=($usersGender=='Not Specified')?'selected="selected"':''?>>Not Specified</option>
</select>

答案 1 :(得分:-1)

最好的

while($row = mysql_fetch_array($result))
{
  if ($sectorActive != $row['idSector']) {
    echo '<option value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
  }else{
    echo '<option selected="selected" value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
  }
}
相关问题