How to retrieve select option?

79 views Asked by At

I am calling selected value twice in PHP instead of once.

Please anyone solve my problem??

Html part:

<select name="mol[]" >
   <option value="">Choose Mode of License</option>
   <option value="standalone">Standalone</option>
   <option value"wan">WAN</option>
   <option value"lan">LAN</option>
</select>

php code:

echo "<td>";
echo "<select name='mol' class='mol'>
     <option value='$row[mol]'>$row[mol]</option>
     <option value='standalone'>Standalone</option>
     <option value='wan'>WAN</option>
     <option value='lan'>LAN</option>
  </select>"; 
echo "</td>";
1

There are 1 answers

1
AH. On BEST ANSWER

Use loop and generate a select box

    //firs create an array with your options 
    $options[] = array(
    'standalone' => 'Standalone',
    'wan'=>'Wan',
    'lan'=>'Lan'
     );

    //if already selected store the key to a variable 
    $selected = @$row['mol'];

    echo "<select name='mol' class='mol'>";
    //generate select box
    foreach($options as $op => $key)
    {
     if($selected == $op)
      {
         echo " <option value='$op' selected = 'selected'>$key</option>";
      }
     else
      {
         echo " <option value='$op' >$key</option>";
      }
    }
   echo "</select>";