Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag 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";
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ellandrd

ASKER

perfect!