Auto select boxes?

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
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Auto select boxes?

Post by alix »

I've searched the forum and found a few posts that gave me ideas..

I have select boxes on my site, if i select an option and update it, it'll send the information to the database, when i return to the page, i would like it to have the option i just entered into the database selected already. Is there a tutorial for this anywhere? or do someone know a bit of code i could take a look at and figure out?

Thanks,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

we've talked about this ad naseum... you have to query the database for the current value. When displaying the values available, you check against the currently stored value, when found, you write in 'selected="selected"' into the option.
alix
Forum Commoner
Posts: 42
Joined: Thu Nov 18, 2004 8:41 am

Post by alix »

alright i think i got it, i used this code; figured i'd post it incase someone else needs it maybe.. if its of any use... its ugly. lol

Code: Select all

$HTTP_POST_VARS;
$result = mysql_query("SELECT * FROM usertable WHERE id = "$id" ");
while($myrow = mysql_fetch_assoc($result))
             {             
     $provider= $myrowї'provider'];
      }  
   ?>
<select name="provider">
<?
$query = mysql_query("SELECT * FROM provider_table ");
while ($row = mysql_fetch_assoc($query)) &#123;
   $provider_db = $row&#1111;'carrier'];
if ($provider_db=="$provider")&#123;
?> 
<option value="<? echo $provider_db; ?>" selected><? echo $provider_db; ?> </option>
<?
&#125; else &#123;
?>
<option value="<? echo $provider_db; ?>"><? echo $provider_db; ?> </option>
<?
&#125;&#125;
?>
</select>
Thanks Feyd. :D
Post Reply