Greetings everyone,

I have been working on a class reunion web site and am trying to build a myprofile.php page to allow our members to update their profile. I have been working on this for several weeks (as time permits) and have run into a couple of problems. I sincerely appreciate any help on the first one. I have a form with a few fields that are drop down menus or select options.

I am doing a query which returns the existing value for each field from the database and populates the input field, which is easy for text input fields, but I can't quite figure it out for drop down menus.

The form does update the database properly, as long as you click one of the options, but it does not properly reflect the already existing value in the database.

My question is, how do properly format the php code to show the correct selected option?

Here is portion of code that I have tried to get to work:

<td width="300px" align="left">
<select name="prefcontact" size="6">
<?php $prefnone_sel = $prefph_sel = $prefpm_sel = $prefpb_sel = $prefemail_sel = $prefimc_sel = $prefpostal_sel = $prefdnc_sel = "";
if ($prefcontact =="") {
   $prefnone_sel = "checked" ;
} elseif ($prefcontact =="ph") {
   $prefph_sel = "checked" ;
} elseif ($prefcontact =="pm") {
   $prefpm_sel = "checked" ;
} elseif ($prefcontact =="pb") {
   $prefpb_sel = "checked" ;
} elseif ($prefcontact =="email") {
   $prefemail_sel = "checked" ;
} elseif ($prefcontact =="imc") {
   $prefimc_sel = "checked" ;
} elseif ($prefcontact =="postal") {
   $prefpostal_sel = "checked" ;
} elseif ($prefcontact =="dnc") {
   $prefdnc_sel = "checked" ;
}
?>
<option value="" <?php echo "$prefnone_sel"; ?> />Please select one
<option value="ph" <?php echo "$prefph_sel"; ?> />Home Phone
<option value="pm" <?php echo "$prefpm_sel"; ?> />Mobile Phone
<option value="pb" <?php echo "$prefpb_sel"; ?> />Business Phone
<option value="email" <?php echo "$prefemail_sel"; ?> />Email
<option value="imc" <?php echo "$prefimc_sel"; ?> />IMC (Messaging Client)
<option value="postal" <?php echo "$prefpostal_sel"; ?> />Postal Mail
<option value="dnc" <?php echo "$prefdnc_sel"; ?> />Do Not Contact
    	</td>

This code allows the visitor to select their preferred method of contact.

This attempt was based on something I read in "Teach Yoursel Php with MySQL" by Nat McBride. A good little book but as with many books it assumes a certain level of programming knowledge.

I have searched this site and many others but have never found this covered.

Thanks again for any help you can offer.

    Write a Reply...