Jump to content

Recommended Posts

 

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]

Link to comment
https://forums.phpfreaks.com/topic/68931-selected-remain-selected/
Share on other sites

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]

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

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

 

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.