Jump to content

pre-populating drop down menus in a form (select)


jeff5656

Recommended Posts

When updating a record, I want the form to be pre-populated, but I want the values in the drop down menu to also show what the record values are.  the following does not work though.  The drop down doesn't display the values, it displays "Please Select" even when there was an actual value for that record:

 

 <th>Sex</th><?php $select = isset($_POST["sex"]) ? $_POST["sex"] : ''; ?>
  <td><select name = "sex" >
      <option value="" <?php if ($select == '') echo 'selected="selected"'; ?>>Please Select</option>
      <option value="m" <?php if ($select == 'm') echo 'selected="selected"'; ?>>Male</option>
      <option value="f" <?php if ($select == 'f') echo 'selected="selected"'; ?>>Female</option>
      </select></td></tr>
  <tr></tr>
  <th>Race</th>
  <td><select name = "race" >
      <option value="" <?php if ($select == '') echo 'selected="selected"'; ?>>Please Select</option>
      <option value="b" <?php if ($select == 'b') echo 'selected="selected"'; ?>>Black</option>
      <option value="w" <?php if ($select == 'w') echo 'selected="selected"'; ?>>White</option>
      <option value="o" <?php if ($select == 'o') echo 'selected="selected"'; ?>>Other</option>
      </select></td></tr>

Hi Jeff,

 

This is what i do:

 

<?php
// gender array //
$gender_array = array("1"=>"Male","2"=>"Female");

$gender = $row['gender']; <-- 1 or 2 coming from mysql (depending on what the user has selected)

// then //
print("<select name='sex'>");
foreach($gender_array as $key => $gender_value)
{
  print("<option value='$key' ");
  
  // if for selected //
  if($key == $gender)
  {
   print(" selected");
  }
  
  print(">$gender_value</option>\n");
  
}
print("</select>\n"); 
?>

 

hope that helps.

 

Graham

Hi,

that doesn't work.  If I have a female it still shows "male" as the default on the drop down.

 

I changed "1" and "2" to "m" and "f", but otherwise kept everything else you had the same.

 

Now if i select female and hit submit, it correctly changes the value (confirmed when I view it in phpmmyadmin), but when I go back to edit the record, the form always displays male as the default.

Hi Mate,

 

this is really the bit we are after:

 

<?php
  // if for selected //
  if($key == $gender)
  {
   print(" selected");
  }
?>

 

 

$gender <-- this is basically the variable your grabbing from mysql in the gender table it will store either the 1 or 2

$key <-- when $gender matches one of the keys (1 or 2) it will echo the selected

 

are you pulling the users gender from mysql at all?

 

Graham

I made a stupid mistake - my value is called "sex" not gender.  Once I changed the $gender to $sex, it works.  Thanks! 

 

Now if only someone could help me with my problem put forth in the javascript forum (subject "2 java scripts in one php file").

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.