...

View Full Version : Echo Session Variable for Drop Down Lists



Sherrie
01-08-2007, 01:21 PM
Hi all!

I have a drop down list that should display the selected value from the stored session variable when the user returns to the page to review his/her response.

I know that I'm meant to echo the selected value, but i'm not quite sure how to go about doing that.

With my current code, it's adding the word SELECTED at the end of the selected option instead of showing it as the selected option.

Here is my code:


<select name="division" id="division" >
<option value="0">---Please Select---</option>
<option value="1">Executive Office Division
<?php if($_SESSION['answer']['division'] == "1") echo "SELECTED"; ?>
</option>
<option value="2">Sales &amp; Marketing Division
<?php if($_SESSION['answer']['division'] == "2") echo "SELECTED"; ?>
</option>
<option value="3">Sales &amp; Operations Division
<?php if($_SESSION['answer']['division'] == "3") echo "SELECTED"; ?>
</option>
<option value="4">Admin &amp; Finance Division
<?php if($_SESSION['answer']['division'] == "4") echo "SELECTED"; ?>
</option>
</select>

Would be great if anyone can help me out with this.

Thanks!

koyama
01-08-2007, 01:48 PM
Tip: Say View > Source in your browser and you'll see something is going wrong with the generation of HTML. To get the e.g. the second option preselected you would write something like this, right?


<select name="division" id="division" >
<option value="0">---Please Select---</option>
<option value="1" selected="selected">Executive Office Division</option>
<option value="2">Sales &amp; Marketing Division</option>
<option value="3">Sales &amp; Operations Division</option>
<option value="4">Admin &amp; Finance Division</option>
</select>

Now in php you could take advantage of looping:


$options[1] = 'Executive Office Division';
$options[2] = 'whatever';
$options[3] = ...
$options[4] = ...

$code = '<select name="division" id="division">'."\n";
$code.= '<option value="0">---Please Select---</option>'."\n";
for($i=1; $i<=4; $i++) {
$selected = ($_SESSION['answer']['division'] == $i) ? ' selected="selected"' : '';
$code.= "<option value=\"$i\"$selected>{$options[$i]}</option>\n";
}
$code.= '</select>'."\n";

echo $code;

Len Whistler
01-08-2007, 09:02 PM
<?php if($_SESSION['answer']['division'] == "1") echo "SELECTED"; ?>

Should be something like.


<?php if($_SESSION['answer']['division'] == "1") echo "$SELECTED"; ?>

You need the $ for the echo statement.

Sherrie
01-08-2007, 09:51 PM
koyoma,

Thanks for your reply and tip!

I agree it would take much less coding with the loop, but I'm still pretty new to PHP, and loops seem rather daunting at the moment, cos I don't understand the lines and what they do. :o

Len Whistler,

Thank you for your reply!

I found out that I had put that echo line in the display section instead of before, which is why it added on to what was displayed. It seems to work without the $ before SELECTED.

Silly me!

But now I've run into another problem.

The value of the division drop down list dynamically populates the dept list which in turn dynamically populates the section list.

When I return to the page to review it, only the division and section drop down list values are selected. How would i need to change the code?

here's the Javascript code that i found on the net:


function dynamicSelect(id1, id2) {
// Feature test to see if there is enough W3C DOM support
if (document.getElementById && document.getElementsByTagName) {
// Obtain references to both select boxes
var sel1 = document.getElementById(id1);
var sel2 = document.getElementById(id2);
// Clone the dynamic select box
var clone = sel2.cloneNode(true);
// Obtain references to all cloned options
var clonedOptions = clone.getElementsByTagName("option");
// Onload init: call a generic function to display the related options in the dynamic select box
sel1.onload=function(){
refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
sel1.onchange = function() {
refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
};
}
}

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
var pattern1 = /( |^)(select)( |$)/;
var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
// Iterate through all cloned options
for (var i = 0; i < clonedOptions.length; i++) {
// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
// Clone the option from the hidden option pool and append it to the dynamic select box
sel2.appendChild(clonedOptions[i].cloneNode(true));
}
}
}
// Attach our behavior onload
window.onload = function() {
dynamicSelect("division", "dept");
dynamicSelect2("dept","section");
}

And here's my HTML with the PHP echo function:


<select name="dept" id="dept">
<option class="0" value="0">---Please Select---</option>
<option class="1" value="0">---Please Select---</option>
<option class="1" value="1"<?php if($_SESSION['answer']['dept'] == "1") echo "SELECTED"; ?>>Not Applicable</option>
<option class="2" value="0">---Please Select---</option>
<option class="2" value="2"<?php if($_SESSION['answer']['dept'] == "2") echo "SELECTED"; ?>>Watchcare Centre Department</option>
<option class="2" value="3"<?php if($_SESSION['answer']['dept'] == "3") echo "SELECTED"; ?>>Marketing Department</option>
<option class="2" value="4"<?php if($_SESSION['answer']['dept'] == "4") echo "SELECTED"; ?>>Creative Department</option>
<option class="3" value="0">---Please Select---</option>
<option class="3" value="5"<?php if($_SESSION['answer']['dept'] == "5") echo "SELECTED"; ?>>Brands Department</option>
<option class="3" value="6"<?php if($_SESSION['answer']['dept'] == "6") echo "SELECTED"; ?>>Retail Department</option>
<option class="4" value="0">---Please Select---</option>
<option class="4" value="7"<?php if($_SESSION['answer']['dept'] == "7") echo "SELECTED"; ?>>HR Department</option>
<option class="4" value="8"<?php if($_SESSION['answer']['dept'] == "8") echo "SELECTED"; ?>>Finance Department</option>
</select>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum