Avatar of ellandrd
ellandrd
Flag for Ireland asked on

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";
PHP

Avatar of undefined
Last Comment
ellandrd

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
amit_g

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ellandrd

ASKER
perfect!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23