SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    865
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Making a DB result the default selection in a drop down

    In my SQL database I have a column called skills. I'm creating a webpage where users can update their information and change their professional skill. The page loads fine, but the drop down menu is not defaulting to what $loginSkill contains. Does this make sense?

    What am I doing wrong in my code below?

    Thanks!


    Code:
    My Skill Test:</b>
    <select name='skills' id='skills'>
    
    <?php
    
    if ($loginSkills == "None") {$sel = "selected";} else {$sel = "";}
    print("<option $sel value='None'>None</option>");
    
    if ($loginSkills == "Aromatherapist") {$sel = "selected";} else {$sel = "";}
    print("<option $sel value='Aromatherapist'>Aromatherapist</option>");
    
    if ($loginSkills == "Chiropractor") {$sel = "selected";} else {$sel = "";}
    print("<option $sel value='Chiropractor'>Chiropractor</option>");
    
    ?>
    
    </select><br>
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    $loginSkill or $loginSkills?
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast

  3. #3
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    865
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here is my revised code and it still doesn't work.

    My Skill Test/b>
    <select name='loginSkill' id='loginSkill'>

    <?php

    if ($loginSkill == "None") {$sel = "selected";} else {$sel = "";}
    print("<option $sel value='None'>None</option>");

    if ($loginSkill == "Aromatherapist") {$sel = "selected";} else {$sel = "";}
    print("<option $sel value='Aromatherapist'>Aromatherapist</option>");

    if ($loginSkill == "Chiropractor") {$sel = "selected";} else {$sel = "";}
    print("<option $sel value='Chiropractor'>Chiropractor</option>");

    ?>

    </select><br>
    Convert your dollars into silver coins. www.convert2silver.com

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    $skill 'none';

    $selected strcasecmp('none',$skill)==0?' selected="selected"':' ';
    print(
    "<option$selected value='Aromatherapist'>Aromatherapist</option>");

    $selected strcasecmp('aromatherapis',$skill)==0?' selected="selected"':' ';
    print(
    "<option$selected value='Aromatherapist'>Aromatherapist</option>");

    $selected strcasecmp('chiropractor',$skill)==0?' selected="selected"':' ';
    print(
    "<option$selected value='Chiropractor'>Chiropractor</option>"); 

  5. #5
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    865
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, but can you help me understand why my code doesn't work? I actually got this example off one of my other websites, where it does work.

    Thanks.
    Convert your dollars into silver coins. www.convert2silver.com

  6. #6
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are your select tags within form tags?

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    You left out the attribute name.

    PHP Code:
    if ($loginSkills == "None") {$sel "selected";} else {$sel "";}
    print(
    "<option $sel value='None'>None</option>"); 
    That results in:

    HTML Code:
    <option "selected" value='None'>None</option>
    What you need is:

    HTML Code:
    <option selected="selected" value='None'>None</option>

  8. #8
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually selected is a boolean attribute, as the OP has it is correct. It is resulting without quotes for me.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Its not correct if its not working. That seems like really lazy mark-up. It probably has something to do with the doc type then and your chosen doc type isn't allowing that lazy way of defining it. I've never seen that attribute present in that context.

  10. #10
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is shown as such in the HTML 4.0 specifications. Am I missing something? Is it different for XHTML, if that is the case, that I don't know?

  11. #11
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Southwest Florida
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think selected vs. selected="selected" is a function of strict vs. transitional. Busboy should probably get in the habit of using the full attribute="value" form. But I also think that's a red herring here...... I would say the first step is to check the value of $loginSkill and make sure it contains the value he expects:
    Code:
    echo $loginSkill;
    busboy, if you add the above code before your select list, does it output the value you expect? If not, then show us the code where $loginSkill is set.

  12. #12
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the clarification.

  13. #13
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    865
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thatnks everyone. I forgot to request the value from the session. Once I fixed that, all is well.

    Thanks.
    Convert your dollars into silver coins. www.convert2silver.com


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
  •