Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
select-option list

Posted by libraryStudent (libraryStudent), 11 July 2003
Hello, I am a new registered member and took Graham's PHP course at Pepperdine University.  

I am trying to create a form with a table containing 2 input boxes, a select-option list, and a submit button.  When a user enters an item in the first input box, selects a period (either weekly, monthly, or annually) in the select-option list, and enters a number in the second input box, and clicks the submit button, a calculation is performed (depending on which option is selected) and the results are displayed on the form and the option that was selected (weekly, monthly, or annually is also displayed selected.)

My question is how do you create select-option lists dynamically and keep a submited option value selected after a user clicks a submit button?

Here is part of my code:



<title>My Bills</title>


<body style="font-family: Arial, Helvetica, sans-serif; color: blue;">

<h1>My Bills</h1>

<form method="POST">



     <th>Period Amount</th>
     <th>Annual Period</th>


//validate period amount

//Initialize period_amount_err_cnt variable and set it to zero initially
$period_amount_err_cnt = $HTTP_POST_VARS['$period_amount_err_cnt'];
$period_amount_err_cnt = 0;

/* for() statement creates table */

for ($i = 1; $i < 2; $i++)
/*Initialize variables, get form field values into PHP variable names appending counter, using the "long" format*/
$item_name = 'item_name'."$i";      
$item_value = $HTTP_POST_VARS[$item_name];
$period_amount = 'period_amount'."$i";
$period_amount_value = $HTTP_POST_VARS[$period_amount];
$choice_name = 'choice'."i";
$choice_value = $HTTP_POST_VARS[$choice_name];
$weekly = $HTTP_POST_VARS[$weekly];
$monthly = $HTTP_POST_VARS[$monthly];
$annually = $HTTP_POST_VARS[$annually];
//create rows
print '<tr>';

print "<td>

<input type=text name=$item_name

print "<td><select name=$choice_value size=1>";

print "<option value=$weekly>weekly";

print "<option value=$monthly>monthly";

print "<option value=$annually>annually";

print "</select></td>";

print "<td><input type=text name=$period_amount value='$period_amount_value'></td>";

if ($period_amount_value != (is_numeric($period_amount_value)))

print ("<td><font color=\"#FF0000\">Amount '$period_amount_value' is not a number</font></td>");
$period_amount_err_cnt++;   //add one to the $period_amount_err_cnt

switch ($choice_value)
case "$choice_value1":
if ((is_numeric($period_amount_value)) && $choice_value1==$weekly)

echo '<option value=$weekly selected>';
$period_amount_value = 52*$period_amount_value;
print ("<td>$period_amount_value</td>");

case "$choice_value2":

if ((is_numeric($period_amount_value)) && $choice_value2==$monthly)

echo '<option value=$monthly selected>';
$period_amount_value = 12*$period_amount_value;
print ("<td>$period_amount_value</td>");
case "$choice_value3":      
if ((is_numeric($period_amount_value)) && $choice_value3==$annually)
echo '<option value=$annually selected>';
$period_amount_value = 1*$period_amount_value;
print ("<td>$period_amount_value</td>");

print '</tr>';





Posted by admin (Graham Ellis), 11 July 2003
The short answer is that you need to put the word SELECTED back into the form when you redisplay it against the appropriate option.

This need to feedback is also vital where you want to give people a chance to correct their entries or develop a form.   I've put up a complete example at goes along the lines you're asking about - it retains the frequency and the amount entered.  Here's the code:


$periods = array("daily" => 365,"weekly" => 52,
               "monthly" => 12 ,"annually" => 1);
$opt1 = "<option value=never>Choose frequency";
$opt2 = $opt1;
foreach (array_keys($periods) as $ptype) {
       $c1 = ($ptype == $_GET[f1]) ? " SELECTED" : "";
       $opt1.= "<option value=$ptype $c1>$ptype";
       $c2 = ($ptype == $_GET[f2]) ? " SELECTED" : "";
       $opt2.= "<option value=$ptype $c2>$ptype";

if ($_GET[v1] > 0 and $_GET[f1] != "never") {
       $result1 = $_GET[v1] * $periods[$_GET[f1]];
       $state = sprintf("%.2f %s is worth %.2f annually",
                       $_GET[v1], $_GET[f1], $result1);
       $result = "First result - $state<br>";
} else{
       $result = "Incomplete data - first test<br>";
       $result1 = 0;
if ($_GET[v2] > 0 and $_GET[f2] != "never") {
       $result2 = $_GET[v2] * $periods[$_GET[f2]];
       $state = sprintf("%.2f %s is worth %.2f annually",
                       $_GET[v2], $_GET[f2], $result2);
       $result .= "Second result - $state<br>";
} else{
       $result .= "Incomplete data - second test<br>";
       if ($result1 == 0) $result = "Results will go here<br>";

<head><title>Which is the better value?</title></head>
<body bgcolor=white>
Comparing payment frequencies
<form>Please select first amount
<input name=v1 value=<?php print ($_GET[v1]) ; ?>>
and frequency <select name=f1><?php print ($opt1)?></select><br>
Please select second amount
<input name=v2 value=<?php print ($_GET[v2]) ; ?>>
and frequency <select name=f2><?php print ($opt2)?></select><br>
Then <input type=submit></form><hr>
<?php print ($result) ; ?><hr>
Copyright, Well House Consultants 2003

Note that I've taken care to display "Your results will go here" the first time through when no data is present.  Also note that if I wanted to make more than two comparisons, I should be using a loop to go through each case - but that would have made the example much harder to follow.  Final note - I should have provided some data validation for the numeric input.

Posted by admin (Graham Ellis), 11 July 2003
Further updated example - using an array to hold up to 10 lines of data and the code's even shorter. See

where the code is


$rows = 10;

$periods = array("daily" => 365,"weekly" => 52,
               "monthly" => 12 ,"annually" => 1);
$total = 0;
for ($k=0; $k<$rows; $k++) {
       $opt[$k] = "<option value=never>Choose frequency";
       foreach (array_keys($periods) as $ptype) {
               $c = ($ptype == $_GET["f$k"]) ? " SELECTED" : "";
               $opt[$k] .= "<option value=$ptype $c>$ptype";
       if ($_GET["v$k"] > 0 and $_GET["f$k"] != "never") {
       $result = $_GET["v$k"] * $periods[$_GET["f$k"]];
       $state[$k] = sprintf("%.2f", $result);
       $total += $result;
} else{
       $state[$k] = "---";
$full = sprintf("%.2f", $total);

<head><title>What is the total?</title></head>
<body bgcolor=white>
Adding up regular incomes
<?php for ($k=0;$k<$rows;$k++) { ?>
<input name=v<?php print $k ;?> value=<?php print ($_GET["v$k"]) ; ?>>
<select name=f<?php print $k ; ?>><?php print ($opt[$k])?></select>
<?php } ?>
Then <input type=submit></form><hr>
Total is <?php print ($full) ; ?><hr>
Copyright, Well House Consultants 2003

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho