Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5
  1. #1
    Senior Member
    Join Date
    Oct 2007
    Posts
    241
    Member #
    15910
    How can I make the drop down menu stay as selected after user hits submit? Right now it works and displays the correct results however once it is ubmitted, the form itself returns to displaying its defaults selections although the correct results are displayed based on the last selection.

  2.  

  3. #2
    Senior Member
    Join Date
    Apr 2005
    Location
    Hatfield, England
    Posts
    855
    Member #
    9790
    You would use php.

    Code:
    <select name="blah">
      <option value="1" <?php if ($_POST['blah'] == 1) print 'selected="selected"'; ?>>Option 1</option>
      <option value="2" <?php if ($_POST['blah'] == 2) print 'selected="selected"'; ?>>Option 1</option>
      <option value="3" <?php if ($_POST['blah'] == 3) print 'selected="selected"'; ?>>Option 1</option>
    </select>

  4. #3
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    As a side note, if this code is (or could be) repeated more than twice I would suggest assigning 'selected="selected"' to a variable and changing Jason's example to read print $variable to be less verbose.

  5. #4
    Senior Member
    Join Date
    Apr 2005
    Location
    Hatfield, England
    Posts
    855
    Member #
    9790
    Usually I use a helper function to do this. However I thought that for simplicity it would be easier to understand if it was just some inline code.

  6. #5
    Senior Member
    Join Date
    Oct 2007
    Posts
    241
    Member #
    15910
    Thanks....I did it as follows:
    echo "<select name=\"month\">";
    $mnts=array(0,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep, Oct,Nov,Dec);
    for ($i=1; $i<13; $i++)
    {
    if($_POST['month']==$i){$sel="selected";}else{$sel=null;}
    echo "<option value=$i $sel>$mnts[$i]</option>";
    }
    echo "</select>";


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:01 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com