根据变量值选择表单中的选择属性

时间:2013-11-22 21:13:37

标签: php html forms

我希望根据变量的值选择表单中的select值。下面是我尝试的代码,虽然我总是在select中得到值female,因为我认为select参数总是被应用。完成此任务的正确方法是什么?

          <li><label for="sex">Sex:</label>
            <select name="sex">
            <option value="male" selected="<?php if(strcmp($Sex, 'male') ==0)echo 'select'; ?>">Male</option>
            <option value="female" selected="<?php if(strcmp($Sex, 'female') ==0)echo 'select'; ?>">Female</option>
            </select></li>

1 个答案:

答案 0 :(得分:1)

   <select>  <option value="male" <?php if($Sex == 'male')echo 'selected="selected"'; ?>>Male</option>
    <option value="female" <?php if($Sex == 'female')echo 'selected="selected"'; ?> >Female</option>
  </select>

Empty selected =“”与selected =“selected”相同