473,394 Members | 1,845 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,394 software developers and data experts.

Defualt Value for drop down list

9
I have a drop down/list and am using a php and a mysql table to store my values. I am displaying an update page that already has the user's number stored in it. I want to default their current number value in the dropdown/list. How do I accomplish this? My code is below. What am I missing ?

<select name="number" class="longfield-select" option value="<?echo "$number?>">

<option value="">Select Number</option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>

</select>
Feb 20 '08 #1
7 1576
TheServant
1,168 Expert 1GB
[HTML]<select name="number" class="longfield-select">

<option value="" <?php if ($number == NULL) { echo("selected=\"selected\""); } ?> >Select Number</option>
<option value="One" <?php if ($number == 1) { echo("selected=\"selected\""); } ?>>One</option>
<option value="Two" <?php if ($number == 2) { echo("selected=\"selected\""); } ?>>Two</option>
<option value="Three" <?php if ($number == 3) { echo("selected=\"selected\""); } ?>>Three</option>

</select>[/HTML]

I'm no pro, and I've probably made some mistakes just because I am saying this, but your php syntax is not very good. Even if you don't want to do what I wrote, just have a look at what is different between my php code and yours.

Regards,
The Servant
Feb 20 '08 #2
jtmphp
9
Thanks for the feedback. However, I wouldn't of posted this question if I knew how to handle this.

Thanks
Feb 20 '08 #3
TheServant
1,168 Expert 1GB
Thanks for the feedback. However, I wouldn't of posted this question if I knew how to handle this.

Thanks
I meant no offense, I started teaching myself php about 3 months ago, so I have a little experience (most of it trial and error)! Is this what you wanted?
Feb 20 '08 #4
jtmphp
9
Yes this will work. Thanks for your help
Feb 20 '08 #5
Markus
6,050 Expert 4TB
Yes this will work. Thanks for your help
While theservants does indeed work, a more effective way would be to loop through the options and use an if to check - this way you arent opening and closing the php parser:
[php]
<select>
<option value="">Select one</option>
<?php
$_n = 4; // users selected number
for($_i = 1; $_i < 6; ++$_i)
{
if($_i == $_n) // if this is the users number...
echo "<option value=\"$_i\" selected=\"selected\">$_i</option>";
else
echo "<option value=\"$_i\">$_i</option>";
}
?>
</select>
[/php]
Feb 20 '08 #6
TheServant
1,168 Expert 1GB
While theservants does indeed work, a more effective way would be to loop through the options and use an if to check - this way you arent opening and closing the php parser:
[php]
<select>
<option value="">Select one</option>
<?php
$_n = 4; // users selected number
for($_i = 1; $_i < 6; ++$_i)
{
if($_i == $_n) // if this is the users number...
echo "<option value=\"$_i\" selected=\"selected\">$_i</option>";
else
echo "<option value=\"$_i\">$_i</option>";
}
?>
</select>
[/php]
Nice! I tried to think of a loop way but still very inexperienced when it comes to that. That's why I stick around here, always learning!
Feb 20 '08 #7
Markus
6,050 Expert 4TB
Nice! I tried to think of a loop way but still very inexperienced when it comes to that. That's why I stick around here, always learning!
Indeed, indeed. It's a wonderful place!

Atleast now you know the logic for making a loop in this circumstance!
Feb 20 '08 #8

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

Similar topics

1
by: Dan | last post by:
This is one that has me stumped and I need an expert's input. Any ideas why the values from the second script-generated drop down list isn't recognized by the script to add time values to the...
3
by: pmud | last post by:
Hi, I have a drop down list bound to a database thorugh a data reader. It reads the customer names from data reader. Now, I want the user to be able to type more than one alphabet & the list...
4
by: Martyn Fewtrell | last post by:
I want to update a database record with a number of fields - doesn't sound to difficult at this stage! I have a system to select the correct record from the database and load the data into a...
3
by: Mike Collins | last post by:
I'm not feeling too smart right now, but I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. Can someone show...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
2
by: Ren | last post by:
Hi all, I am a bit new to PHP and SQL so this may seem like a dumb question. I have already created a drop down list as part of a form which is automatically populated with values taken from a...
0
by: weiwei | last post by:
Hi here is my scenario, I create a drop down list in itemtemplate.(that drop down is created from db), after user click edit command, my ideal plan is have another drop down list in...
3
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the...
15
by: ajos | last post by:
Hello all, Im making a dependent drop down for my application for filtering purpose. I have populated the drop down from the database and retrieved in the html. <tr> <td valign="top"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.