SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2011
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to set default value on form drop downs

    I have a form with select drop down options. The form allows users to edit their profile, so I want to dynamically set the default value equal to their value in the database. I have the database part working but I'm having problems figuring out the html part for some reason.

    What should I have for the first line where %s is their current value.

    HTML Code:
    <option value='%s' selected='selected'>%s</option>
    Here is the select tag
    HTML Code:
                       <select name='state'>
    	                <option value='%s' selected='selected'>%s</option> 
    	                <option value='Alabama'>Alabama</option> 
    	                <option value='Alaska'>Alaska</option> 
    	                <option value='Arizona'>Arizona</option> 
    	                <option value='Arkansas'>Arkansas</option> 
    	                <option value='California'>California</option> 
    	                <option value='Colorado'>Colorado</option> 
    	                <option value='Connecticut'>Connecticut</option> 
    	                <option value='Delaware'>Delaware</option> 
    	                <option value='District Of Columbia'>District Of Columbia</option> 
    	                <option value='Florida'>Florida</option> 
    	                <option value='Georgia'>Georgia</option> 
    	                <option value='Hawaii'>Hawaii</option> 
    	                <option value='Idaho'>Idaho</option> 
    	                <option value='Illinois'>Illinois</option> 
    	                <option value='Indiana'>Indiana</option> 
    	                <option value='Iowa'>Iowa</option> 
    	                <option value='Kansas'>Kansas</option> 
    	                <option value='Kentucky'>Kentucky</option> 
    	                <option value='Louisiana'>Louisiana</option> 
    	                <option value='Maine'>Maine</option> 
    	                <option value='Maryland'>Maryland</option> 
    	                <option value='Massachusetts'>Massachusetts</option> 
    	                <option value='Michigan'>Michigan</option> 
    	                <option value='Minnesota'>Minnesota</option> 
    	                <option value='Mississippi'>Mississippi</option> 
    	                <option value='Missouri'>Missouri</option> 
    	                <option value='Montana'>Montana</option> 
    	                <option value='Nebraska'>Nebraska</option> 
    	                <option value='Nevada'>Nevada</option> 
    	                <option value='New Hampshire'>New Hampshire</option> 
    	                <option value='New Jersey'>New Jersey</option> 
    	                <option value='New Mexico'>New Mexico</option> 
    	                <option value='New York'>New York</option> 
    	                <option value='North Carolina'>North Carolina</option> 
    	                <option value='North Dakota'>North Dakota</option> 
    	                <option value='Ohio'>Ohio</option> 
    	                <option value='Oklahoma'>Oklahoma</option> 
    	                <option value='Oregon'>Oregon</option> 
    	                <option value='Pennsylvania'>Pennsylvania</option> 
    	                <option value='Rhode Island'>Rhode Island</option> 
    	                <option value='South Carolina'>South Carolina</option> 
    	                <option value='South Dakota'>South Dakota</option> 
    	                <option value='Tennessee'>Tennessee</option> 
    	                <option value='Texas'>Texas</option> 
    	                <option value='Utah'>Utah</option> 
    	                <option value='Vermont'>Vermont</option> 
    	                <option value='Virginia'>Virginia</option> 
    	                <option value='Washington'>Washington</option> 
    	                <option value='West Virginia'>West Virginia</option> 
    	                <option value='Wisconsin'>Wisconsin</option> 
    	                <option value='Wyoming'>Wyoming</option>
    	            </select>

  2. #2
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,741
    Mentioned
    32 Post(s)
    Tagged
    1 Thread(s)
    Well it not the HTML it's the thinking behind it. You DONT want to have a separate option tag for this. Even if it worked it would mean you would have the users location appear TWICE ( once as the selected and the other in the list).

    What you want to do instead is ADD "selected='selected'" to the option tag that has the same value as the users current value.

  3. #3
    SitePoint Member
    Join Date
    Jun 2012
    Location
    Orange County California
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using PHP to pull the data or doing an AJAX call? Have to know what your using on the server side to help.
    Quote Originally Posted by aaron4osu View Post
    I have a form with select drop down options. The form allows users to edit their profile, so I want to dynamically set the default value equal to their value in the database. I have the database part working but I'm having problems figuring out the html part for some reason.

    What should I have for the first line where %s is their current value.

    HTML Code:
    <option value='%s' selected='selected'>%s</option>
    Here is the select tag
    HTML Code:
                       <select name='state'>
    	                <option value='%s' selected='selected'>%s</option> 
    	                <option value='Alabama'>Alabama</option> 
    	                <option value='Alaska'>Alaska</option> 
    	                <option value='Arizona'>Arizona</option> 
    	                <option value='Arkansas'>Arkansas</option> 
    	                <option value='California'>California</option> 
    	                <option value='Colorado'>Colorado</option> 
    	                <option value='Connecticut'>Connecticut</option> 
    	                <option value='Delaware'>Delaware</option> 
    	                <option value='District Of Columbia'>District Of Columbia</option> 
    	                <option value='Florida'>Florida</option> 
    	                <option value='Georgia'>Georgia</option> 
    	                <option value='Hawaii'>Hawaii</option> 
    	                <option value='Idaho'>Idaho</option> 
    	                <option value='Illinois'>Illinois</option> 
    	                <option value='Indiana'>Indiana</option> 
    	                <option value='Iowa'>Iowa</option> 
    	                <option value='Kansas'>Kansas</option> 
    	                <option value='Kentucky'>Kentucky</option> 
    	                <option value='Louisiana'>Louisiana</option> 
    	                <option value='Maine'>Maine</option> 
    	                <option value='Maryland'>Maryland</option> 
    	                <option value='Massachusetts'>Massachusetts</option> 
    	                <option value='Michigan'>Michigan</option> 
    	                <option value='Minnesota'>Minnesota</option> 
    	                <option value='Mississippi'>Mississippi</option> 
    	                <option value='Missouri'>Missouri</option> 
    	                <option value='Montana'>Montana</option> 
    	                <option value='Nebraska'>Nebraska</option> 
    	                <option value='Nevada'>Nevada</option> 
    	                <option value='New Hampshire'>New Hampshire</option> 
    	                <option value='New Jersey'>New Jersey</option> 
    	                <option value='New Mexico'>New Mexico</option> 
    	                <option value='New York'>New York</option> 
    	                <option value='North Carolina'>North Carolina</option> 
    	                <option value='North Dakota'>North Dakota</option> 
    	                <option value='Ohio'>Ohio</option> 
    	                <option value='Oklahoma'>Oklahoma</option> 
    	                <option value='Oregon'>Oregon</option> 
    	                <option value='Pennsylvania'>Pennsylvania</option> 
    	                <option value='Rhode Island'>Rhode Island</option> 
    	                <option value='South Carolina'>South Carolina</option> 
    	                <option value='South Dakota'>South Dakota</option> 
    	                <option value='Tennessee'>Tennessee</option> 
    	                <option value='Texas'>Texas</option> 
    	                <option value='Utah'>Utah</option> 
    	                <option value='Vermont'>Vermont</option> 
    	                <option value='Virginia'>Virginia</option> 
    	                <option value='Washington'>Washington</option> 
    	                <option value='West Virginia'>West Virginia</option> 
    	                <option value='Wisconsin'>Wisconsin</option> 
    	                <option value='Wyoming'>Wyoming</option>
    	            </select>

  4. #4
    SitePoint Member
    Join Date
    May 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah your thinking about this the wrong way, add "selected='selected'" to the option tag that matches the users selected value.
    Some development tips and tricks can be found on my development website


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
  •