Page 1 of 1

how to retain selected value from dropdown list

Posted: Tue Aug 10, 2010 4:25 am
by raj86
Hello friends
i am showing the related values of my selected items on the same page. but when i select the item, the system by default show me the first value of the database. can i retain the selected item on the same page?????

Code: Select all

<?php
$query="SELECT sno,item FROM desktops";

$result = mysql_query ($query);
echo "<select name='name' value=''>Item Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[sno]>$nt[item]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
?>
thank you

Re: how to retain selected value from dropdown list

Posted: Tue Aug 10, 2010 6:32 am
by JakeJ
Try this:

Code: Select all

<?php
function selected($db_value, $actual_value) {
		If ($db_value == $actual_value) {
			$selected = "selected";
		}
		Else {
			$selected = "";
		}
	return $selected;
}
?>
//use the function as follows:
<option value="1" <?php echo selected(1, $row['frequency']); ?>>Once per</option>