0

我有这个下拉菜单,用户可以选择多个选项,如果表单出现错误,我如何在提交按钮后将选定的值保留在表单上

<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>
4

2 回答 2

1

instead of

$_POST['oca'] == $odrop1

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

in_array($odrop1,$_POST['oca']) 
于 2012-12-27T06:24:59.073 回答
0

TRY THIS-

echo '<option value="' . $odrop1 . '"' . (is_array($_POST['oca']) && in_array($odrop1,$_POST['oca'] ) ? ' selected' : '') . '>' . $odrop1 . '</option>';
于 2012-12-27T06:25:30.823 回答