proctk Posted September 12, 2007 Share Posted September 12, 2007 Hi I have the below select box. When the user selects a value I want it to remain selected. It currently changes selected the first value. any ideas how to do this thanks <select name="numItems" id="numItems" onchange="this.form.submit()"> <option value="21">00</option> <option value="20">01</option> <option value="19">02</option> <option value="18">03</option> <option value="17">04</option> <option value="16">05</option> <option value="15">06</option> <option value="14">07</option> <option value="13">08</option> <option value="12">09</option> <option value="11">10</option> <option value="10">11</option> <option value="09">12</option> <option value="08">13</option> <option value="07">14</option> <option value="06">15</option> <option value="05">16</option> <option value="04">17</option> <option value="03">18</option> <option value="02">19</option> <option value="01">20</option> </select> [code] [/code] Quote Link to comment https://forums.phpfreaks.com/topic/68931-selected-remain-selected/ Share on other sites More sharing options...
teng84 Posted September 12, 2007 Share Posted September 12, 2007 I dont know what method your are trying to use but this is the good example $selected = ($month == $given )?'selected':''; $stringMENU .= "<option value='" .$month. "' ".$selected .">" .$month. "</option>\n\t"; Quote Link to comment https://forums.phpfreaks.com/topic/68931-selected-remain-selected/#findComment-346484 Share on other sites More sharing options...
proctk Posted September 12, 2007 Author Share Posted September 12, 2007 This is what I have but when I select a value the loop never ends it keeps displaying rows and it freezes my browzer <?php $items = $_POST['numItems']; foreach(array("","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15", "16","17","18","19","20") as $value) { echo "<option"; if($value == $items) { echo " selected='selected'"; } echo ">$value</option>\n"; } ?> </select> </form> <?php if(isset($_POST['numItems'])){ $numItems = $_POST['numItems']; }else{ $numItems = 21; } while ($numItems <= 20 ) { ?> <dl> <dt class="floatLeft"><dt><dd class="addPadding"><select name="wholeNum[]"> <option value="00">00</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">0</option> <option value="10">10</option> </select> <select name="fraction[]"> <option value="00">00</option> <option value="1/16">1/16</option> <option value="1/8">1/8</option> <option value="1/4">1/4</option> <option value="1/2">1/2</option> <option value="3/4">3/4</option> </select> <select name="meassurement[]" id="measurement"> <option value="">Choose</option> <option value="Teaspoon(s)">Teaspoon(s)</option> <option value="Tablespoon(s)">Tablespoon(s)</option> <option value="Cup(s)">Cup(s)</option> <option value="Pint(s)">Pint(s)</option> <option value="Quart(s)">Quart(s)</option> <option value="Pound(s)">Pound(s)</option> <option value="Pintch">Pintch</option> <option value="Ounce">Ounce</option> </select> <input name="ingredient" type="text" /></dd> <?php $numItems --; ?> <?php }?> [code] [/code] Quote Link to comment https://forums.phpfreaks.com/topic/68931-selected-remain-selected/#findComment-346486 Share on other sites More sharing options...
teng84 Posted September 12, 2007 Share Posted September 12, 2007 try first <?php $items = $_POST['numItems']; $test = array("","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20"); foreach($test as $value) { $selected = ($value == $items)?'selected':''; $stringMENU .= "<option value='" .$value. "' ".$selected .">" .$value. "</option>\n\t"; ?> </select> </form> <?php if(isset($_POST['numItems'])){ $numItems = $_POST['numItems']; }else{ $numItems = 21;//<-------------------------------wrong this wont do anything } while ($numItems <= 20 ) { ?> <dl> <dt class="floatLeft"><dt><dd class="addPadding"><select name="wholeNum[]"> <option value="00">00</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">0</option> <option value="10">10</option> </select> <select name="fraction[]"> <option value="00">00</option> <option value="1/16">1/16</option> <option value="1/8">1/8</option> <option value="1/4">1/4</option> <option value="1/2">1/2</option> <option value="3/4">3/4</option> </select> <select name="meassurement[]" id="measurement"> <option value="">Choose</option> <option value="Teaspoon(s)">Teaspoon(s)</option> <option value="Tablespoon(s)">Tablespoon(s)</option> <option value="Cup(s)">Cup(s)</option> <option value="Pint(s)">Pint(s)</option> <option value="Quart(s)">Quart(s)</option> <option value="Pound(s)">Pound(s)</option> <option value="Pintch">Pintch</option> <option value="Ounce">Ounce</option> </select> <input name="ingredient" type="text" /></dd> <?php $numItems --; ?> <?php }?> Quote Link to comment https://forums.phpfreaks.com/topic/68931-selected-remain-selected/#findComment-346490 Share on other sites More sharing options...
proctk Posted September 12, 2007 Author Share Posted September 12, 2007 thank you for the post, I'm not following what i need to change Quote Link to comment https://forums.phpfreaks.com/topic/68931-selected-remain-selected/#findComment-346494 Share on other sites More sharing options...
teng84 Posted September 12, 2007 Share Posted September 12, 2007 your code declare an array inside the foreach that maybe cause a never ending loop ant this if(isset($_POST['numItems'])){ $numItems = $_POST['numItems']; }else{ $numItems = 21;//<-------------------------------wrong this wont do anything } while ($numItems <= 20 ) { ?> no need for the else just put the code inside that if (all data for this --->>> while ($numItems <= 20 ) { ?>) should be if(isset($_POST['numItems'])){ $numItems = $_POST['numItems']; while ($numItems <= 20 ) { ?> <dl> <dt class="floatLeft"><dt><dd class="addPadding"><select name="wholeNum[]"> <option value="00">00</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">0</option> <option value="10">10</option> </select> <select name="fraction[]"> <option value="00">00</option> <option value="1/16">1/16</option> <option value="1/8">1/8</option> <option value="1/4">1/4</option> <option value="1/2">1/2</option> <option value="3/4">3/4</option> </select> <select name="meassurement[]" id="measurement"> <option value="">Choose</option> <option value="Teaspoon(s)">Teaspoon(s)</option> <option value="Tablespoon(s)">Tablespoon(s)</option> <option value="Cup(s)">Cup(s)</option> <option value="Pint(s)">Pint(s)</option> <option value="Quart(s)">Quart(s)</option> <option value="Pound(s)">Pound(s)</option> <option value="Pintch">Pintch</option> <option value="Ounce">Ounce</option> </select> <input name="ingredient" type="text" /></dd> <?php $numItems --; }?> Quote Link to comment https://forums.phpfreaks.com/topic/68931-selected-remain-selected/#findComment-346497 Share on other sites More sharing options...
proctk Posted September 12, 2007 Author Share Posted September 12, 2007 here is what I have, I'm back to the same challenge, its freezing and the loop is not ending. thank you for your help <form name="numItems" method="post" action="<?php $PHP_SELF; ?>"> <select name="numItems" id="numItems" onchange="this.form.submit()"> <?php $items = $_POST['numItems']; foreach(array("","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15", "16","17","18","19","20") as $value) { echo "<option"; if($value == $day) { echo " selected='selected'"; } echo ">$value</option>\n"; } ?> </select> </form> <?php if(isset($_POST['numItems'])){ $numItems = $_POST['numItems']; while ($numItems <= 20 ) { ?> <dl> <dt class="floatLeft"><dt><dd class="addPadding"><select name="wholeNum[]"> <option value="00">00</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">0</option> <option value="10">10</option> </select> <select name="fraction[]"> <option value="00">00</option> <option value="1/16">1/16</option> <option value="1/8">1/8</option> <option value="1/4">1/4</option> <option value="1/2">1/2</option> <option value="3/4">3/4</option> </select> <select name="meassurement[]" id="measurement"> <option value="">Choose</option> <option value="Teaspoon(s)">Teaspoon(s)</option> <option value="Tablespoon(s)">Tablespoon(s)</option> <option value="Cup(s)">Cup(s)</option> <option value="Pint(s)">Pint(s)</option> <option value="Quart(s)">Quart(s)</option> <option value="Pound(s)">Pound(s)</option> <option value="Pintch">Pintch</option> <option value="Ounce">Ounce</option> </select> <input name="ingredient" type="text" /></dd> <?php $numItems --; ?> <?php }}?> Quote Link to comment https://forums.phpfreaks.com/topic/68931-selected-remain-selected/#findComment-346504 Share on other sites More sharing options...
teng84 Posted September 12, 2007 Share Posted September 12, 2007 i have change this line on my previous post foreach(array("","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15", "16","17","18","19","20") as $value) look on the first code i post and please try to check it Quote Link to comment https://forums.phpfreaks.com/topic/68931-selected-remain-selected/#findComment-346507 Share on other sites More sharing options...
proctk Posted September 12, 2007 Author Share Posted September 12, 2007 as you probably noticed, im a rookie at this stuff. I cannot get the select option to display with the code that you posted Quote Link to comment https://forums.phpfreaks.com/topic/68931-selected-remain-selected/#findComment-346517 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.