This may already be out there in the forums, but I really don't know what to search for to find it.

What I'm looking for is a way to have a dropdown filled with values, and have it select a value from the database and display it.

For example. On a website, when a user edit's their account, they can change the language. This is displayed in a dropdown box. They seach and select English to be their language. Next time they go in to edit their settings, I want English to be the value that is already selected in that dropdown. This can be done, just need a push in the right direction? Thanks.

Recommended Answers

All 3 Replies

Here's one way

<select name="selLanguage">
while ($row = mysql_fetch_assoc($result))
	<option <?
	if($row['langid'] == $userslangid)
		echo 'selected="selected" '; 
	?>value="<? echo $row['langid']; ?>"><? echo $row['langname']; ?></option>

Am having the same issue but i have tried the following code
<SELECT NAME="type">
<option <?php if($type ==large) print "SELECTED"?> VALUE="Large">Large</optin>
<option <?php if($type ==medium) print "SELECTED"?> VALUE="medium">Mediu</option>
<option <?php if($type ==large) print "SELECTED"?> VALUE="Large">Small</option>
May problem is i want my default option to be SELECT TYPE and whenever a user select another option like Large,and if the form returns error message(because of other input which i have not presented here) then his/her selected option remain while the form display error message. Any help would be ppreciated

syntax is important $type is a variable, the values are quoted,
html should be lower case,

earlier in the code there must have been something creating the value in $type from the post/get array, assuming the form is submitted by the post method and the form is submitted to itself for processing

<?php if(isset($_POST['type'])){$type=$_POST['type'];}?>
<form action="" method="post">
 <select name="type">
   <option <?php if(!$type) echo 'selected="selected"'; ?> value="">SELECT TYPE</option>
    <option <?php if($type =='large') echo 'selected="selected"'; ?> value="large">Large</option>
    <option <?php if($type =='medium') echo 'selected="selected"'; ?> value="medium">Medium</option>
    <option <?php if($type =='small') echo 'selected="selected"'; ?> value="small">Small</option>

the value of the default, unselected option, is usually left blank,
'Small' is not equal to 'small'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.