SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2003
    Location
    San Diego
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Maintaining dropdown list value on submit

    I have a HTML dropdown list like this:

    <select name="tool">
    <option value="0.3">Cobalt</option>
    <option value="0.35">Coated</option>
    <option value="0.8">Uncoated</option>
    <option value="1">TiAlN coated</option>
    </select>

    When the form is submitted the dropdown resets to the first option. I’d like to maintain the value on postback like in .NET. Thanks for any help -E

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    <option selected><? echo $_POST['tool']; ?></option>

    You might need to do an if(isset or register a session variable.

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <select name="tool">
    <option value="0.3" <?php echo $_GET['tool']=='0.3'?'selected':''?>>Cobalt</option>
    <option value="0.35" <?php echo $_GET['tool']=='0.35'?'selected':''?>>Coated</option>
    <option value="0.8" <?php echo $_GET['tool']=='0.8'?'selected':''?>>Uncoated</option>
    <option value="1" <?php echo $_GET['tool']=='1'?'selected':''?>>TiAlN coated</option>
    </select>
    Use $_POST instead of $_GET if you are doing a POST request.

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    texas
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well,
    instead of just outputting raw html for the menu, you'll have to dynamically create it after you submit... something like:

    Code:
    // define the options for the select menu
    $opts = array( array("name" => "Cobalt", "value" => "0.3"),
                   array("name" => "Coated", "value" => "0.35"),
                   array("name" => "Uncoated", "value" => "0.8"),
                   array("name" => "TiAlN coated", "value" => "1"));
    
    // get the form input
    $tool = isset($_REQUEST["tool"]) ? $_REQUEST["tool"] : "";
    
    // now build the select
    echo "<select name='tool'>";
    foreach($opts as $opt){
       if($opt["val"] == $tool){
           $selected = "SELECTED";
       }else{
           $selected = "";
       }
       echo "<option value='" . $opt["val"] . "' $selected>";
       echo $opt["name"];
       echo "</option>";
    }
    echo "</select>";
    hope this helps
    - Adam
    free online calendar: http://inversiondesigns.com
    includes multiple calendars/users/groups, task manager,
    email and sms reminders, permissions (sharing), etc.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    texas
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmph, i apparently type too slow, heh.
    free online calendar: http://inversiondesigns.com
    includes multiple calendars/users/groups, task manager,
    email and sms reminders, permissions (sharing), etc.

  6. #6
    SitePoint Member
    Join Date
    Jul 2003
    Location
    San Diego
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right on, thank you all for your input: drzoid's solution works perfectly! Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •