troubleshooting Question

setting selected index using PHP

Avatar of ellandrd
ellandrdFlag for Ireland asked on
PHP
2 Comments1 Solution340 ViewsLast Modified:
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
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros