By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,025 Members | 2,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,025 IT Pros & Developers. It's quick & easy.

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

P: 18
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
Share this Question
Share on Google+
8 Replies


P: 29
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
Expert 100+
P: 938
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
Expert 5K+
P: 5,821
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

P: 18
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
Expert 5K+
P: 5,821
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
Expert 2.5K+
P: 3,235
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

P: 18
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
Expert 100+
P: 404
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

Post your reply

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