php combobox默认选中

use*_*875 1 php mysql combobox default

设想:

字段可以是YES或NO - > Active:Yes | 活跃:不

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

我想在表单中显示结果,我应该能够编辑它并再次保存在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>

}
Run Code Online (Sandbox Code Playgroud)

不要重视显示"扇区"甚至查询等信息.

恢复:

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

谢谢 :)

cst*_*rat 5

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

echo '<option value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
Run Code Online (Sandbox Code Playgroud)

具有:

echo '<option selected="selected" value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
Run Code Online (Sandbox Code Playgroud)

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

你可能有这样的事情:

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>';
  }
}
Run Code Online (Sandbox Code Playgroud)

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

<select name="gender">
  <option>Male</option>
  <option>Female</option>
  <option selected="selected">Other</option>
  <option>Not Specified</option>
</select>
Run Code Online (Sandbox Code Playgroud)

因此,如果您有查询提取记录,并且您希望确保选择了正确的值.你需要有一个随每条记录而变化的变量 - 你需要在那个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>
Run Code Online (Sandbox Code Playgroud)