SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to echo out drop-down box selection?

    Hi All,

    I'm trying to echo out a value that was inserted into my database with a drop down box.

    For example, when I click 'edit' for a row, I want that value to be the value pre-selected in the edit form drop-down box. However, it defaults to the first option in the 'select' tag on my edit form.

    My question:
    How do I do I accomplish this? I give my current code below which is not able to do this function. Thanks!

    Code:
    <select name="dropdown" value="<?php htmlout($value); ?>" />
    <option>1</option>
    <option>2</option>
    <option>3</option>
    </select>

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,603
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The value= needs to go on each optiion - it isn't valid on the select.

    You need to set selected on the option that you want selected.

    PHP Code:
    <select name="dropdown" />
    <option<?php if ('1'==htmlout($value)) echo ' selected="selected"'?>>1</option>
    <option<?php if ('2'==htmlout($value)) echo ' selected="selected"'?>>2</option>
    <option<?php if ('3'==htmlout($value)) echo ' selected="selected"'?>>3</option>
    </select>
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Felgall -

    Unfortunately, I'm not able to make this work. When I use this code my dropdown box becomes empty.

    Here is the code I'm using. Am I doing something wrong here?

    PHP Code:
    <select name="type"/>
                    <option<?php if ('Article'==htmlout($type))echo ' selected="selected"'?>>Article</option>
                    <option<?php if ('Preface'==htmlout($type)) echo ' selected="selected"'?>>Preface</option>
                    <option<?php if ('Review'==htmlout($type)) echo ' selected="selected"'?>>Review</option>
            </select>
    Seems I'm following your code. If I code ' <option&nbsp;<?php if...>> ' then the drop down is populated but it does not echo back the chosen option.

    Thanks for your help. BTW, I'm using Firefox, if that matters.

    Thanks!

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,048
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    $types = array('Article','Preface','Review');
    $type = isset($_POST['type']) && in_array($_POST['type'],$types)?$_POST['type']:'Article';

    echo 
    '<select name="type">';
    foreach(
    $types as $option) {
         echo 
    '<option value="'.$option.'"'.(strcmp($option,$type)==0?' selected="selected"':'').'>'.$option.'</option>';
    }
    echo 
    '</select>'

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, oddz!

    The code worked and it functions great.

    I should say that I removed the line that defines "$type" since I already had that in my controller - took me a bit to realize this since it wasn't working at first.

    But, using my controller definition and removing that line it works great!

    Cheers


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
  •