| |||||||||||
| |||||||||||
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: <html> <head> <title>My Bills</title> </head> <body style="font-family: Arial, Helvetica, sans-serif; color: blue;"> <h1>My Bills</h1> <form method="POST"> <table> <tr> <th>Item</th> <th>Period</th> <th>Period Amount</th> <th>Annual Period</th> </tr> <?php //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 value='$item_value'></td>"; 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>'; } ?> </table> Thanks. 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 http://www.wellho.net/demo/t2.php4 goes along the lines you're asking about - it retains the frequency and the amount entered. Here's the code: Code:
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. Seehttp://www.wellho.net/demo/t3.php4 where the code is Code:
This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |