raj
raj

Reputation: 129

Keeping selected dropdown value

I have this drop-down and user can select multiple options ,how can i keep selected value on form after submit button, if error comes on form

<select  onclick="document.getElementById('cand_qual4').style.display='none'; " name="oca[]" id="oca"  multiple="multiple">
<?php
      $odrop = array('B COM','M COM','BBA','MBA','LLB','LLM','CPA','CIMA','MS FINANCE','DISA','CISA','OTHER');

      foreach ($odrop as $odrop1)
      {
        echo '<option value="' . $odrop1 . '"' . (isset($_POST['oca']) && in_array($odrop1,$_POST['oca'])  ? ' selected' : '') . '>' . $odrop1 . '</option>';
      } 
 ?>         
</select>

Upvotes: 0

Views: 307

Answers (2)

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16076

TRY THIS-

echo '<option value="' . $odrop1 . '"' . (is_array($_POST['oca']) && in_array($odrop1,$_POST['oca'] ) ? ' selected' : '') . '>' . $odrop1 . '</option>';

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27835

instead of

$_POST['oca'] == $odrop1

condition as $_POST['oca'] would be an array, try

in_array($odrop1,$_POST['oca']) 

Upvotes: 1

Related Questions