Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form with two drop down lists

Status
Not open for further replies.

jbeetz1

Programmer
Nov 28, 2000
60
0
0
US
I have a form for simplicity has two drop down list as indicated below with a selected value for each:

Maximum number of Primary Drivers per Team:&nbsp;<select size="1"
name=max_drivers>< option value="1" selected><? echo $rs[1] ?><? echo $max ?></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">9</option>
<option value="10">10</option>
&lt;\SELECT&gt;</select><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Maximum number of Back-up Drivers per Team:&nbsp;<select size="1"
name=max_backup_drivers>< option value="1" selected><? echo $rs[2] ?><? echo $max ?></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">9</option>
<option value="10">10</option>

The values are changed by the user and submitted to a MYSQL database. The form also has a select statement to return the save values.

The question is how do I display the return values to the user and if the user only changes one of the values then submits again then the unchanged drop down list seems to be defaulting to the selected value rather than the returned value.

Thanks
Jerry
 
Hi,

If i'm right in saying, you want to ensure that each value stored in the database is the selected value for each drop down menu. Data should first be stored to the database if the user updates it, then reopen/open the database to build the select menu based on the db data. If you want to keep it simple, you could do the following:

Code:
<?php
$select = $_POST['select'];
$submit = $_POST['submit'];

if ($submit) {
     //Open database and save $select value.
     //Close database
}

//Open database and read saved select value into $dbselect.
//Close database
?>

<form action="script.php" method="post">
     <select name="select">
          <option value="<?php echo $dbselect; ?>" selected><?php echo $dbselect; ?></option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
     </select>
     <input type="submit" name="submit" value="submit" />
</form>

A drawback is that the selected value will be printed twice within the select menu. If there isn't a $dbselect value, then the selected value will be blank.

If you were to build the entire select menu server side (php), then you could simply check if the current option being printed equals $dbselect, and if it does, print selected at the end of the html tag:

Code:
<select name="select">
<?php
for ($optioncount = 1; $counter <= 3; $counter += 1) {
     $selected = '';
     if ($optioncount == $dbselect) {
          $selected = ' selected';
     }
     echo "<option value=\"$optioncount\"$selected>$optioncount</option>";
}
?>
</select>

Hope this helps,

Chris
 
@OP

first, please always place your code within [ignore]
Code:
[/ignore] tags. in their absence, code is more difficult to read.

second: html attribute values should canonically be enquoted with double quotes.

third: the format for selecting an option in a select box is to provide an attribute value pair.
Code:
<option value="somevalue" selected="selected">some text</option>

although other variations may work, they are not correct.

what you are asking is to have a sticky form. Briefly, for a given form like the following:

Code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="mySelect">
<option value="orange">orange</option>
<option value="apple">apple</option>
<option value="raisin">raisin</option>
</select>
<input type="submit" value="submit" name="submit"/>
</form>

you would need to add some preprocessing code as follows:

Code:
<?php
$mySelect = empty($_POST['mySelect']) ? 'orange' : trim($_POST['mySelect']);
?>
and then it is easier to profile your option values rather than hard code the output

Code:
$mySelectValues = array('orange'=>'orange', 'apple'=>'apple', 'raisin'=>'raisin');

then outputting the form with sticky values is straightforward
Code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="mySelect">
<?php
foreach ($mySelectValue as $val=>$text){
   $selected = ($mySelect == $val) 'selected="selected"' : '';
   echo "\r\n\t<option value=\"$val\" $selected>$text</option>";
} //end foreach ?>
</select>
<input type="submit" value="submit" name="submit"/>
</form>

if you are really using numbers in your options then consider disambiguating by forcing into strings and using === instead of == otherwise you run the risk of inferred boolean true/false being compared rather than a proper value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top