Imagine that:
A field that could be YES or NO -> Active: Yes | Active: No
So, i got a query that returns it for each client.
I want to show the results in a form, and I should be able to edit that and save on BD again.
I can almost do it, but I need that the default value be wich was already saved on BD, and what I have atm seems show the last row or something.
I want that show the current status or be able to change it for (Yes or NO)
It goes like this for the combobox:
$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>
}
Dont give importance of info displayed like "sector" or even the query etc..
Resume:
I want that combobox could display the current value as default and give a list of anothers possible values to change.
Thank you :)
I might have missed the point of the question, but are you asking how you can make a from field appear as being selected? Simple modify this line:
echo '<option value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
To have:
echo '<option selected="selected" value="'.$row['idSector'].'">'.$row['idSector'].' - '.$row['Nomesec'].'</option>';
If that is the particular option you want selected...
You might have something like this:
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>';
}
}
Ok, so if you want a select field to have a default value you need to have selected="selected"
as a paramater value in the <option>
element. As per your example like this:
<select name="gender">
<option>Male</option>
<option>Female</option>
<option selected="selected">Other</option>
<option>Not Specified</option>
</select>
So if you have a query pulling records and you want to make sure you have the right value selected. You need to have a variable that changes with each record - and you need to install some logic in that HTML insert, like this: (this example will output the same HTML as the example above...)
<?
$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>
THE BEST
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>';
}
}