how to retain selected value from dropdown list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raj86
Forum Commoner
Posts: 25
Joined: Tue Sep 29, 2009 12:28 am

how to retain selected value from dropdown list

Post 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
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: how to retain selected value from dropdown list

Post 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>

Post Reply