Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Selected Drop Down Values

    while ($row = mysql_fetch_row($result))
    {
    $str .='<option value='.$row[0].'';
    if ($row[0] == $UserID)
    {
    $str .='selected="selected"';
    }
    $str .= '>'.$row[1].'</option>';
    }
    echo $str;

    My code is not working. I retrieved everything from the database. Let's say the drop down list has 10 values and UserID=4. But when I view the page, the 4th option is not selected. Instead the 1st option is selected. Why is that so?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    It's more of a PHP question and definitely not Javascript. You should have posted it there. Ask the mod to move it and don't cross-post.

    I don't know PHP but the first thing you need to do is to view the generated HTML source to check if the HTML for the dropdown is correct. You might be missing some quotes...

  • #3
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In an html select you don't set selected="selected". You just put selected without quotes or anything within the brackets.

    like

    PHP Code:
    <select name="name">
        <
    option value="value1">Value 1</option>
        <
    option value="value2" selected>Value 2</option>
    </
    select

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Quote Originally Posted by kyletxag
    In an html select you don't set selected="selected". You just put selected without quotes or anything within the brackets.

    like

    PHP Code:
    <select name="name">
        <
    option value="value1">Value 1</option>
        <
    option value="value2" selected>Value 2</option>
    </
    select
    In XHTML, attribute minimization is forbidden.

  • #5
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i stand corrected.


  •  

    Posting Permissions

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