0

我有一个 html 表单,我一直在使用它来 POST 到 php 表单,这会将发布的数据保存到数据库中。当我单击编辑时,我现在正在将数据加载回表单中,这适用于文本字段,因为我可以使用

value="<?php echo $somefield ?>" 

问题是我不确定如何重新选择我的下拉菜单。菜单看起来像这样,用于选择一个类别,但是当页面重新加载时没有任何反应,下拉菜单也没有重新加载。

    <select name="category" id="category">
    <option>- Select A Category -</option>
      <optgroup label="Tractors">
      <option value="Tractor">Tractor</option>
      <option value="Compact">Compact</option>
    <option value="Forklift">Forklift</option>
    <option value="Load All">Load All</option>
    <option value="Loader">Loader</option>
    <option value="Maniscopic">Maniscopic</option>
    <option value="Masted">Masted</option>
    <option value="Piviot">Piviot</option>
    <option value="Telescopic">Telescopic</option>
    <option value="UTV">UTV</option>
      </optgroup>
      <optgroup label="Machinery">
      <option value="Attachments">Attachments</option>
      <option value="Bale Handling">Bale Handling</option>
      <option value="Cultivation Equipment">Cultivation Equipment</option>
      <option value="Drills">Drills</option>
      <option value="Feeders">Feeders</option>
      <option value="Fertlizer Spreaders">Fertlizer Spreaders</option>
      <option value="Grass Harrows">Grass Harrows</option>
      <option value="Grassland Equipment">Grassland Equipment</option>
      <option value="Hedge Cutters">Hedge Cutters</option>
      <option value="Mowers">Mowers</option>
      <option value="Muck Spreaders ">Muck Spreaders </option>
      <option value="Ploughs">Ploughs</option>
      <option value="Post Hammers">Post Hammers</option>
      <option value="Power Harrows">Power Harrows</option>
      <option value="Rakes">Rakes</option>
      <option value="Rolls">Rolls</option>
      <option value="Rotovators">Rotovators</option>
      <option value="Shredders">Shredders</option>
      <option value="Splitters">Splitters</option>
      <option value="Sprayers">Sprayers</option>
      <option value="Spreaders">Spreaders</option>
      <option value="Tankers">Tankers</option>
      <option value="Tedders">Tedders</option>
      <option value="Toppers">Toppers</option>
      <option value="Trailers">Trailers</option>
      <option value="Wrappers">Wrappers</option>

      </optgroup>
      <optgroup label="Forklifts">
      </optgroup>
    </select>
4

2 回答 2

2

构建列表时,所选选项应具有 selected="selected" 属性。

于 2012-12-18T19:13:35.160 回答
2
<?php

  echo '<select>';
  foreach( $names as $name )
    echo '<option value="'.$name.'" '.($name==$selected?' selected="selected"':'').'>'.$name.'</option>'

  echo '</select>';

?>
于 2012-12-18T19:15:07.287 回答