Page 1 of 1

Auto select boxes?

Posted: Thu Feb 03, 2005 12:31 pm
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,

Posted: Thu Feb 03, 2005 12:46 pm
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.

Posted: Thu Feb 03, 2005 1:17 pm
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