Prellyan Posted May 23, 2011 Share Posted May 23, 2011 Hey there, i have a cookie which echos ok but would like to know how to automatically select the corresponding option from a drop down list when the page loads. Normal List <form id="switchform"> <select name="switchcontrol" size="1" class="topusernav" onChange="chooseStyle(this.options[this.selectedIndex].value, 60)"> <option value="none">-----</option> <option value="noir">Noir</option> <option value="crimson">Crimson</option> <option value="forrest">Forrest</option> <option value="ocean">Ocean</option> <option value="petal">Petal</option> </select> </form> works fine but <form id="switchform"> <select name="switchcontrol" size="1" class="topusernav" onChange="chooseStyle(this.options[this.selectedIndex].value, 60)"> <option value="none" <?php if (!(strcmp("none", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>-----</option> <option value="noir" <?php if (!(strcmp("noir", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Noir</option> <option value="crimson" <?php if (!(strcmp("crimson", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Crimson</option> <option value="forrest" <?php if (!(strcmp("forrest", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Forrest</option> <option value="ocean" <?php if (!(strcmp("ocean", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Ocean</option> <option value="petal" <?php if (!(strcmp("petal", "$_COOKIE[\"mysheet\"];")) {echo "selected=\"selected\"";} ?>>Petal</option> </select> </form> gives me a whitespace error any thoughts?? thanks Quote Link to comment https://forums.phpfreaks.com/topic/237208-select-from-drop-down-using-a-cookie/ Share on other sites More sharing options...
sunfighter Posted May 23, 2011 Share Posted May 23, 2011 Problems with your compare php missing a closing ). $_COOKIE is a string, no need to quote it. use this. if (!(strcmp("none", $_COOKIE["mysheet"]))){echo 'selected="selected"';} BTW: wouldn't <?php if($_COOKIE["mysheet"] == 'none'){echo 'selected="selected"';} ?> be work better?? Quote Link to comment https://forums.phpfreaks.com/topic/237208-select-from-drop-down-using-a-cookie/#findComment-1219114 Share on other sites More sharing options...
Prellyan Posted May 23, 2011 Author Share Posted May 23, 2011 thanks for that i appreciate you help not really used cookies before since i had no need to how would your suggestion be implemented on the page though? Quote Link to comment https://forums.phpfreaks.com/topic/237208-select-from-drop-down-using-a-cookie/#findComment-1219128 Share on other sites More sharing options...
Prellyan Posted May 23, 2011 Author Share Posted May 23, 2011 sweet got it working thanks! Quote Link to comment https://forums.phpfreaks.com/topic/237208-select-from-drop-down-using-a-cookie/#findComment-1219131 Share on other sites More sharing options...
Psycho Posted May 23, 2011 Share Posted May 23, 2011 No need for all that superfluous code. There is a much more elegant and easily maintainable solution. <?php $switchControlsList = array( 'noir' => 'Noir', 'crimson' => 'Crimson', 'forrest' => 'Forrest', 'ocean' => 'Ocean', 'petal' => 'Petal' ); $switchOptions = ''; foreach($switchControlsList as $value => $label) { $selected = (strcmp($value, $_COOKIE['mysheet']) ? ' selected="selected"' : ''; $switchOptions .= "<option value=\"{$value}\"{$selected}>{$label}</option>\n"; } ?> <form id="switchform"> <select name="switchcontrol" size="1" class="topusernav" onChange="chooseStyle(this.options[this.selectedIndex].value, 60)"> <option value="">-----</option> <?php echo $switchOptions; ?> </select> </form> NOTES: 1. Don't use "none" for a "non-selection", just use an empty string - it is a more logical choice 2. No need to have logic on the first option to "select" it since it is supposed to be the default value. if none of the other options are explicitly selected, the first value will be selected by default. 3. The code is built so all you have to do is modify the array of list values. It is built to use the array index as the value and the array value as the select label. But, since you are using the same text for both you could just use array values and not specify the indexes - just use strtolower() when echoing the value for the options. But, I would leave the array in that format as it is more conducive if you are using a database. Quote Link to comment https://forums.phpfreaks.com/topic/237208-select-from-drop-down-using-a-cookie/#findComment-1219132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.