473,287 Members | 1,515 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,287 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 2487
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.