473,394 Members | 1,802 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.

The <select> tag doesn't support a value option

I have a PHP/mySQL database running, and I've realized I'm come across an awkward little bug.

I have a form where a user fills out their information using populated drop down boxes. This information gets stored in the database. Then, if a user wants to edit their information later, they can go to the Edit Details page which calls up a page almost identical to the original form when they first set themselves up. Of course, the person's information is pulled from the database, and is echoed as the values of the form elements.

My issue is that the drop down boxes do not echo the value They are updated in the database correctly, but the drop downs don't know how to select the appropriate values.

The <select> tag doesn't support a value option, and I can't hardcode the selected="selected" into any specific <option> tag because the input from the database needs to determine which one that is.

Any suggestions anyone has would be greatly appreciated. Thanks!
Jun 28 '07 #1
8 2727
Try this
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $sql="select name from table name";
  3. execute the query
  4. $row=mysql_fetch_array($res);
  5. ?>
  6. <select>
  7. <option value="<?=$row['name']?>" selected></option>
  8. </select>
Jun 28 '07 #2
nathj
938 Expert 512MB
Try this
<?
$sql="select name from table name";
execute the query
$row=mysql_fetch_array($res);
?>
<select>
<option value="<?=$row['name']?>" selected></option>
</select>
Alternatively you can use selected="selected" in the option tag of the select element. This works just fine.

So as you build up the select using your database you can add this line to the item that is stored against the user.

Cheers
nathj
Jun 28 '07 #3
pbmods
5,821 Expert 4TB
Heya, dkate.

Expand|Select|Wrap|Line Numbers
  1. foreach($dbresults as $row => $data)
  2.     echo "<option value=\"{$data['rowid']}\"" . (($data['rowid'] === $selectedValue)
  3.         ? ' selected="selected"'
  4.         : ''
  5.     ) . ">{$data['rowname']}</option>\n";
  6.  
If that is not an option, you could add some JavaScript somewhere after you create the SELECT element that runs through the options for your SELECT and sets the selected property to true if its value matches.
Jun 28 '07 #4
Thanks All

I run the test and i see it's selected the right value, but still not display it.
Is anybody know why it's display?
Jun 28 '07 #5
pbmods
5,821 Expert 4TB
Heya dkate.

I run the test and i see it's selected the right value, but still not display it.
Is anybody know why it's display?
Please elaborate. What do you mean by 'selected the right value', and 'still not display it'?
Jun 28 '07 #6
Motoma
3,237 Expert 2GB
Thanks All

I run the test and i see it's selected the right value, but still not display it.
Is anybody know why it's display?
Perhaps you could post the relevant code?
Jun 28 '07 #7
OK, I know where is my problem is.
I use jave script to display value on this form from other form
document.form.fieldname.value=firstformValue

i can display this value on textbox, but not on select box. I think it's possible, but i don't know how to do this.
Is anybody know how to pass value the right way
Jun 28 '07 #8
Purple
404 Expert 256MB
Hi dkate777,

you need to build the select statement with all of the options as you do on the initial form and append selected="selected"

take a look at the following function:

[PHP]//*
//* function - displays the job cost codes
//*
function job_cost_cat_select($data)
{
$code_select = "<tr><td>Cost type :</td><td><select name=\"jcexp_code\">
<option value=\"0\" selected=\"selected\">Not selected</option>";
$rows = 0;
$count = count($data);
while($rows < $count)
{
if(isset($data[$rows][0])) $code_select = $code_select . "<option value=\"" .
rtrim($data[$rows][0]) . "\">" . rtrim($data[$rows][1]) . "</option>";
$rows++;
}
$code_select = $code_select . "</select></td></tr>";
if (isset($_POST['jcexp_code']))
{
$code_select = str_replace("<option value=\"".$_POST['jcexp_code']."\">",
"<option value=\"".$_POST['jcexp_code']."\" selected=\"selected\">",$code_select);
$code_select = str_replace("<option value=\"0\" selected=\"selected\">",
"<option value=\"0\">",$code_select);
}

return($code_select);
}[/PHP]

Hope this helps

Regards Purple
Jun 28 '07 #9

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

Similar topics

3
by: Dennis M. Marks | last post by:
I have a problem with the following code. It generates a <FORM><SELECT><OPTION> list. There is no problem in the generating. The problem is in the execution as follows. It works fine in Mac IE...
2
by: abs | last post by:
Hello everybody. A piece of html code: <form ... > <select ....> <option .... <option .... </select> <select ....>
0
by: rayone | last post by:
Hi folks. I need advice. 2 options, which do you think is the better option to display/retrieve/report on the data. Keep in mind reporting (Crystal), SQL Performance, VB Code, usability,...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
3
by: i_dvlp | last post by:
I'm trying to replicate a fancy drop-down control (MS-egads!) with form <select><option> It doesn't look like you can specity width as an attribute or define width with CSS. It looks like my...
3
by: veg_all | last post by:
Say I have: <select> <option value='a' > First <option value='b' > Second <option value='c' > Third </select> Is there a way I can access the values First, Second and Third from an array ?...
5
by: Isha | last post by:
Hi all, I wanted to change the background color for only the first option in a select box, but following changed the background color for the whole dropdown box. <select name="alltags"...
2
by: msg2ajay | last post by:
hi all, I have a problem with <html:select> i am getting a value from the database but it is not setting to my <select value>. Can any bady tellme what is the problem. My code is given...
1
by: volynetsv | last post by:
Hello. I have a small problem, here's code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html...
4
by: html | last post by:
Hello all, I need to change the colour of my text. For the paragraph I do document.fgColor ="blue" ; but how do I do this for the select tags? ....and I prefer it to apply to all select tags...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.