Jump to content

select from drop down using a cookie


Prellyan

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/237208-select-from-drop-down-using-a-cookie/
Share on other sites

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??

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.