www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Making the listbox ​​have a specific value

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    13

    resolved [RESOLVED] Making the listbox ​​have a specific value

    Hi Friends... I have a list:

    <select name="Codigos" id="Codigos" >
    <option value="1">En Transito</option>
    <option value="2">Afianzando</option>
    <option value="3">Habitual</option>
    <option value="4" >Preferente</option>
    </select>


    I need the list to be positioned at a certain value that I get from a database, for example, suppose the value obtained is 3, then the list must be positioned at "Habitual"

    Can anybody help me?
    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,174
    Add selected="selected" to the desired <option> tag.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2012
    Posts
    13
    Yes... but.. I need to do this automatically. The value list "selected" depends on the value of another variable.
    eJ: var x = 2 -> list must be positioned in "Afianzado" (automatically)
    var x = 3 -> list must be positioned in "Habitual"

  4. #4
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    when you're outputting the dropdown just do a check:

    PHP Code:
    $options = array(
       
    1=>'En Transito',
       
    2=>'Afianzando',
       
    3=>'Habitual',
       
    4=>'Preferente',
    );

    $x 2;

    echo 
    "<select>\n";
    foreach( 
    $options as $k => $v ) {
       echo 
    "<option value='$k'";
       if( 
    $x == $k ) {
          echo 
    " selected='selected'";
       }
       echo 
    ">$v</option>\n";
    }
    echo 
    "</select>\n"
    ~Ryan
    www.rdennispallas.com <-- Personal Site, changing regularly cuz its ugly.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles