Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating dropdown list with certain item selected

Status
Not open for further replies.

kellie2

Technical User
Feb 6, 2006
7
US
Hi and thanks in advance to anyone who can help me.

I have two php pages. The first page contains all records in a database the user can then select a record and edit it - bringing them to the editRecord page. On the edit record page the information about the record once again appears in text boxes to allow the user to change whatever they wish however a few fields are made up of dropdownlists - which are also populated from a database.

- My Problem is: on the editRecord page I want the dropdown list to have the item selected that was previously saved in the database. I have tried several different things but nothing is working for me.

Here is the code I am using:

<?PHP
//sql statement to read the value that are currently saved for the record
$sql= "Select * from redo where counter ='$counter'";
$result=mysql_query($sql, $db);
if($myrows = mysql_fetch_array($result)){
do{
//$date does not use dropdown list
$date=$myrows["Date"];
//$type will use a dropdown list
$type=$myrows["Type"];
}while($myrows=mysql_fetch_array($result));
}
?>

<Table align ="center">
<tr>
<tr></tr>
//date field is populated
<td colspan =2 align="Right"><B>Date:</B></td><td><input type ="TEXT" name="date" value="<?PHP echo "$date"?>";></td>

</tr>

<tr>
<tr></tr>
<td colspan =2 Align ="Right"><B>Type:</B></td>
<td>
//code to populate the dropdown list
<?PHP
$db = mysql_connect("12.1.0.0", "root");
mysql_select_db("RedoDatabase", $db);
$sql ="SELECT Type From typedescription";
$result=mysql_query($sql, $db);
$rowResult2=mysql_fetch_assoc($result);
$totalRows2=mysql_num_rows($result);
//I am not sure if the selected ='.$type.' is correct here?
echo "<select name = type selected ='.$type.'>";
while(list($type)=mysql_fetch_row($result)){
echo "<option value='$type'>$type</option>\n";
}

echo "</select>";
?>
</td>
 
try this
Code:
<form action="<?=$_SERVER[PHP_SELF]?>" method="post">
Choose one: 
<select name="typevalue">
<?
//check whether the form has been submitted and grab select value
$typevalue = isset($_POST['typevalue']) ? trim ($_POST['typevalue']) : "";
$result = mysql_query("Select `type-value`, `type-text` from table");

//iterate through the recordset
while ($row = mysql_fetch_assoc($result)){
 $selected = ($row['type-value'] == $typevalue) ? "selected" : "";
 echo '<option value="'.$row['type-value'].'" $selected >'.$row['type-text'].'</option>';
}
?>
</select>
 
Ok I will try this thanks for the reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top