often you need to "remember" what a user inputs in a form.
that's not a
problem with textfields, but with selectboxes it's a bit harder...
“keep selected combobox” makes use of the built-in PHP functions
in_array( ) and
count( ).
01 <?php
02
03 // you need the values of your combobox in an array
04 $country_values = array('us','de','uk','fr','gb');
05
06 if(isset($_POST['country']) && in_array($_POST['country'], $country_values))
07 {
08 $selected_country = $_POST['country'];
09 $country_output = 'you selected "'.$_POST['country'].'"';
10 }else{
11 // input default value, if empty the first variable will be shown
12 $selected_country = '';
13 $country_output = 'please select an option';
14 }
15
16 $option_num = count($country_values);
17
18 echo '
19 <form method="post" action="./">
20 <label>'.$country_output.'</label>
21 <select name="country">';
22
23 for($x = 0; $x < $option_num; $x++)
24 {
25 // print the options
26 echo '
27 <option value="'.$country_values[$x].'"'.($country_values[$x] == $selected_country ?
28 ' selected="selected"' : '').'>'.$country_values[$x].'</option>';
29 }
30
31 echo '
32 </select>
33 <input type="submit" value="check it out" />
34 </form>';
35
36 ?>