setting a default selection of drop down box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    setting a default selection of drop down box

    Hi all

    I have a drop down box getting values from mysql, mysql contains all months and an id for each e.g. 01 -12

    I want to get the month it is currently as the pre-selected value in my drop down box

    I use $numbermonth = date("m");

    So if it's january the id field will be 01 and the above function will return 01.

    Now, the research I have done on default selection tells me it is something to do putting selected somewhere. I have tried to add an if statement within this while statement to test if $data2[id] == $numbermonth but with no success.

    Code:
    while ($data2=mysql_fetch_assoc($result2))
     {
     echo "<option value=\"", $data2['month'] selected, "\">", $data2['month'], "</option>" . "<br />";
     
     }
     ?>
    anyone help me please with the syntax?

    Regards

    B
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what does var_dump($data2['id']) give? (don't forget to quote the array key)

    further, your XHTML* is not correct. it should look like
    Code:
    <option value="..." selected="selected">...</option><br />
    // or
    <option value="...">...</option><br />
    maybe you can find a friend in printf()

    regards

    * if you use the MIME type "text/html" it doesn't matter, because it will be treated as HTML

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      This part:
      [code=php]"<option value=\"", $data2['month'] selected, "\">"[/code]
      Doesn't look quite right.

      Are you trying to add "selected" to the output, or are you trying to print a variable or constant named "selected"?

      If it is the former, your syntax is off. The "selected" attribute is out of place, it should be inside the quotes, not outside them.
      And Dormilich is of course right. The attribute should have a value.
      Even tho some attributes may work without a value in some browsers, they should ALWAYS be used with a value. It's just good practice, and it *ensures* cross-browser compatibility.

      If it's the latter, the syntax is also off. You would have to add a comma, and a $ if it is a variable.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by Atli
        Even tho some attributes may work without a value in some browsers, they should ALWAYS be used with a value.
        when using XHTML—contrary to HTML—an error will be thrown for not using attribute values. (the XML will not be well-formed)

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by Dormilich
          when using XHTML—contrary to HTML—an error will be thrown for not using attribute values. (the XML will not be well-formed)
          Good point.
          True XHTML sites are rare today. They are usually passed with the text/html header, which of course will cause the site to be parsed as HTML rather than as XML, as they should be.

          Wonder if IE will fail on these errors to, once they catch up and start recognizing XHTML, or if they will stubbornly try to render them anyways.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by Atli
            True XHTML sites are rare today.
            I've seen the YSOD quite often lately..... but it helps.

            Comment

            Working...