473,411 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

setting a default selection of drop down box

153 100+
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.

Expand|Select|Wrap|Line Numbers
  1. while ($data2=mysql_fetch_assoc($result2))
  2.  {
  3.  echo "<option value=\"", $data2['month'] selected, "\">", $data2['month'], "</option>" . "<br />";
  4.  
  5.  }
  6.  ?>
anyone help me please with the syntax?

Regards

B
Jan 23 '09 #1
5 2499
Dormilich
8,658 Expert Mod 8TB
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
Expand|Select|Wrap|Line Numbers
  1. <option value="..." selected="selected">...</option><br />
  2. // or
  3. <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
Jan 23 '09 #2
Atli
5,058 Expert 4TB
Hi.

This part:
Expand|Select|Wrap|Line Numbers
  1. "<option value=\"", $data2['month'] selected, "\">"
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.
Jan 23 '09 #3
Dormilich
8,658 Expert Mod 8TB
@Atli
when using XHTML—contrary to HTML—an error will be thrown for not using attribute values. (the XML will not be well-formed)
Jan 23 '09 #4
Atli
5,058 Expert 4TB
@Dormilich
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.
Jan 23 '09 #5
Dormilich
8,658 Expert Mod 8TB
@Atli
I've seen the YSOD quite often lately..... but it helps.
Jan 23 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: deko | last post by:
I populate a drop-down list like this: echo "<select>"; foreach ( $ip_list as $var ) { echo "<option>"; echo $var; echo "</option>"; } echo "</select>";
8
by: JayB | last post by:
I am creating an online survey for one of our grants and one of the items to be answered is county (U.S.). The county is to be in a drop-down menu to prevent typos and counties from other states by...
3
by: Todd D. Levy | last post by:
I have a number of Drop Down Combo Boxes where users select a country; 99% of the time the selection will be "United States". I want to use VBA code in the New Record Event to populate this field...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
1
by: Null | last post by:
Hello -- Is there a way to prevent the autopostback of a drop down list if there is a "default" item in the list -- example, a drop down list where the first item says "Select One" and the...
2
by: ponyeyes | last post by:
Hi There, I am a bit of a newbie to PHP programming and I would like to know how I can place a selected drop down option into a PHP variable and then produce an sql query which incorporates this...
4
by: sialater | last post by:
Hello, I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have...
2
kmartinenko
by: kmartinenko | last post by:
Hi everyone, I have several combo boxes on my form, and while I have designated a column head, I cannot figure out how to default to the column head value. What I really want is for all of my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.