简体   繁体   中英

restoring multi-select selections in a list box in PHP/HTML

so I have a multi-select listbox...

<select id="extfeat" name="extfeat[]" size="6" multiple="">  
    <option value="Wheel_Chair Accessible">Wheel Chair Accessible</option>  
    <option value="Fruit_Trees">Fruit Trees</option>   
    <option value="Large_Trees">Large Trees</option>   
    <option value="Rolling">Rolling</option>   
    <option value="Other">Other</option>   
    <option value="Level">Level</option>   
    <option value="Wooded">Wooded</option>   
    <option value="Outbuildings">Outbuildings</option>   
    <option value="Gazebo">Gazebo</option>   
    <option value="Workshop Area">Workshop Area</option>   
    <option value="Sauna">Sauna</option>   
    <option value="Courtyard">Courtyard</option>   
    <option value="Lake_Access">Lake Access</option>   
    <option value="Tillable">Tillable</option>   
    <option value="Deck">Deck</option>   
    <option value="Lakeview">Lakeview</option>   
    <option value="Propane_Tank_-_Leased">Propane Tank - Leased</option>   
    <option value="Dock">Dock</option>   
    <option value="Landscaped">Landscaped</option>  
    <option value="Propane_Tank - Owned">Propane Tank - Owned</option>   
    <option value="Fenced_Yard">Fenced Yard</option>   
    <option value="Water_Frontage">Water Frontage</option>   
    <option value="Concrete_Parking">Concrete Parking</option>   
    <option value="Pond">Pond</option>   
    <option value="Tennis_Court">Tennis Court</option>   
    <option value="Circle_Drive">Circle Drive</option>   
    <option value="Horses_Allowed">Horses Allowed</option>   
    <option value="Patio_Enclosed">Patio Enclosed</option>   
    <option value="Sprinkler_System">Sprinkler System</option>   
    <option value="Brick_Trim">Brick Trim</option>   
    <option value="Guest_House">Guest House</option>   
    <option value="Terraced">Terraced</option>   
    <option value="Hot_Tub">Hot Tub</option>   
    <option value="Outdoor_Sign">Outdoor Sign</option>   
    <option value="Garden">Garden</option>   
    <option value="Above_ground_pool">Above ground pool</option>   
    <option value="Ground_Lvl_Access">Ground Lvl Access</option>   
    <option value="Handicap_Access">Handicap Access</option>   
    <option value="Patio">Patio</option>  
    <option value="Garage_Opener">Garage Opener</option>   
    <option value="In-ground_Pool">In-ground Pool</option>   
    <option value="Greenhouse">Greenhouse</option>   
    <option value="Pasture">Pasture</option>   
    <option value="Shed">Shed</option>   
    <option value="Boat_House">Boat House</option>   
    <option value="Antenna">Antenna</option>   
    <option value="Generator">Generator</option>   
    <option value="Asphalt_Parking">Asphalt Parking</option>   
    <option value="Sloping">Sloping</option>   
    <option value="Porch">Porch</option>   
    <option value="Satellite_Dish">Satellite Dish</option>   
    <option value="Screened_Porch">Screened Porch</option>   
    <option value="Underground_Pet_Fence">Underground Pet Fence</option>   
    <option value="Pool">Pool</option> 

  </select>          

so lets say a user selects 7 options and does a search...my search has a refine option and this along with many other fields are saved and stored in a hidden div that can be brought up again by pressing a button...

the problem is that the extfeats[] variable isn't retaining the selections after the POST...i need to somehow transfer the existing multiselections as the defaults.

If you know which options were selected you can mark them as selected by default with: selected="selected" :

<select id="extfeat" name="extfeat[]" size="6" multiple="">  
  <option value="Wheel_Chair Accessible">Wheel Chair Accessible</option>  
  <option value="Fruit_Trees" selected="selected">Fruit Trees</option>
  // ..
</select>

On a minor note multiple="" should probably be multiple="multiple" .

ok...well it turned out to be easier then i thought...

<select id="extfeat" name="extfeat[]" size="6" multiple="">  
  <option value="Wheel_Chair Accessible" <? if (in_array("Wheel_Chair Accessible",$extfeat)) echo "selected" ?> >Wheel Chair Accessible</option>  
  <option value="Fruit_Trees" <? if (in_array("Fruit_Trees",$extfeat)) echo "selected" ?>>Fruit Trees</option>  
  ... 

thxs to Frits van Campen for your gentle push in the right direction ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM