setting selected index using PHP

ive got a simple PHP page with a few selectboxes.

what im trying to do is set the selected index of the gender select box on event of an error during validation.

e.g gender and country are both required, i only select gender and submit form.  if check if gender is set and not empty. if true, i check if the country is set and not empty.  for this example, pretend it return false.

if false i show the form again with a error message prompting the user to select their country, but my gender selectbox is back to selected index of zero.

how can i check and set the selected index of the gender selectbox using PHP?

what i did try was this:

$m_gender_id = $_POST['m_gender_id'];

if($m_gender_id == 'male')
      $sel = 'selected';
else
      $sel = 'selected';

but this wont work

here is my code:

$html .= '<dt><label for="m_gender_id"><strong>My gender is :</strong></label></dt>'."\n";
$html .= '<dd>'."\n";
$html .= '<select name="m_gender_id">'."\n";
$html .= '<option value="0">Select</option>'."\n";
                  
while($genderRows = mysql_fetch_array($genderResults))
{
      $html .= '<option value="'.$genderRows['g_id'].'">'.$genderRows['gender'].'</option>'."\n";
}

$html .= '</select>'."\n";
$html .= '</dd>'."\n";
LVL 16
ellandrdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amit_gCommented:
Do it this way ...

$m_gender_id = $_POST['m_gender_id'];

$html .= '<dt><label for="m_gender_id"><strong>My gender is :</strong></label></dt>'."\n";
$html .= '<dd>'."\n";
$html .= '<select name="m_gender_id">'."\n";
$html .= '<option value="0">Select</option>'."\n";
               
while($genderRows = mysql_fetch_array($genderResults))
{
      if($m_gender_id == $genderRows['g_id'])
           $sel = ' selected';
      else
           $sel = ' ';

      $html .= '<option value="'.$genderRows['g_id'].'"'.$sel.'>'.$genderRows['gender'].'</option>'."\n";
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ellandrdAuthor Commented:
perfect!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.