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

Set the selected text for option box of a menu..

Having major issues with this simple task, and I cant work out why its
not doing as it should/expected.

Basically, ive got a drop down box with the added bit of php:
<select name="title" id="title">
<?php
if($_SESSION['title'] != "") {
echo '<option value="'. $_SESSION['title'] .'"
selected="selected"></option>';
}
else {
echo "<option value=\"\" selected></option>";
}
?>

<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
<option>Dr</option>
</select>
Basically, if the user submits a form, the title is stored in the
session, this works fine, and then when returned to the page, the value
should be filled in.

Yet, when they return to the page, there value they selected is not
shown in this box, instead, theres just a gap. The source seems
correct, and this is whats shown:

<select name="title" id="title">
<option value="Mr" selected="selected"></option>

so it is working like that, but still not showing anything.

Any ideas please?

Oct 19 '06 #1
2 2105
Advo wrote:
<snip>
Yet, when they return to the page, there value they selected is not
shown in this box, instead, theres just a gap. The source seems
correct, and this is whats shown:

<select name="title" id="title">
<option value="Mr" selected="selected"></option>

so it is working like that, but still not showing anything.
Because you have made an HTML markup error with the OPTION element. You
are missing the 'displayed' text...

<option value="parsedValue">Displayed Value</option>

Your code:
<?php
if($_SESSION['title'] != "") {
echo '<option value="'. $_SESSION['title'] .'"
selected="selected"></option>';
}
...
Needs to be:

if($_SESSION['title'] != "") {
echo '<option value="'. $_SESSION['title'] .'"selected="selected">' .
$_SESSION['title'] .'</option>'; }
....

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Oct 19 '06 #2
On 19 Oct 2006 04:53:58 -0700, in comp.lang.php you wrote:
>Having major issues with this simple task, and I cant work out why its
not doing as it should/expected.

Basically, ive got a drop down box with the added bit of php:
<select name="title" id="title">
<?php
if($_SESSION['title'] != "") {
echo '<option value="'. $_SESSION['title'] .'"
selected="selected"></option>';
}
else {
echo "<option value=\"\" selected></option>";
}
?>

<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
<option>Dr</option>
</select>
Basically, if the user submits a form, the title is stored in the
session, this works fine, and then when returned to the page, the value
should be filled in.

Yet, when they return to the page, there value they selected is not
shown in this box, instead, theres just a gap. The source seems
correct, and this is whats shown:

<select name="title" id="title">
<option value="Mr" selected="selected"></option>

so it is working like that, but still not showing anything.

Any ideas please?
It looks like your HTML syntax is the problem:
<option value="Mr">Mr</option>

I generally code selects like:
<?
$title_array = array ('Mr','Mrs','Miss','Ms','Dr');
foreach ($title_array as $title) { ?>
<option
value=<?=$title?>"<?=(in_array($_SESSION['title'],$title_array))?'
selected':''?>><?=$title?></option>
<? } ?>
Oct 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: McKirahan | last post by:
Firefox does not reflect selected option via innerHTML How do I get Firefox to reflect selected option values? <html> <head> <title>FFinner.htm</title> <script type="text/javascript">...
2
by: ste33 | last post by:
Hi, I spent the last hour or so trying to find a simple solution to this but with no success. I'd like to have a select box in a form where a user could select "other" if no option is the...
2
by: Stephen Miller | last post by:
When I dynamically populate a HtmlSelect combo box, the Value property consistently fails to return the item selected, defaulting instead to the first item in the list. For example: Protected...
1
by: phpnewb | last post by:
Hi, I know i'm doing it wrong, but I'm using a while loop right now to create several instances of a drop down menu. It gives me undesirable results. Can you tell me the right way to do it. Below are...
6
by: artev | last post by:
if I have a select with more options, how I can know if is there an option selected; is necessary a cycle? or is there an inner property?
6
by: dragze | last post by:
Hi, right i have a menu box wich allows the user to change what stylesheet the website uses. Now this all works just the way it shud in FF, but not in safari or IE, when u select ur option from the...
1
by: roland-d | last post by:
Hello all, Toggling divs is a popular subject and I have found plenty of information and was able to make it work. There is only 1 thing I am still stuck with for quite some time. I have a...
1
by: sparksol | last post by:
I have a form with a drop down box. If you select an option in the drop down box (depending which option is selected) one or two textbox(es) and a submit button display. I would like to keep the...
3
by: azura | last post by:
<?php require_once('Connections/mas.php'); ?> <?php //initialize the session if (!isset($_SESSION)) { session_start(); } // ** Logout the current user. ** $logoutAction =...
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
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...
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...

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.