tefuzz Posted April 20, 2009 Share Posted April 20, 2009 I have a form. It has 4 different <select> in it. If the lists are all valid (an item is selected) but there are other errors, how would I make PHP keep the selection when the page refreshes to show the fields with errors. Right now my <select> are borught in by a function like this: function getDays ($name, $tabIndex) { echo ("\n\n<select name=\"$name\" id=\"$name\" tabindex=\"$tabIndex\">\n <option selected=\"day\" value=\"\">"); $days = array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"); foreach ($days as $day) { echo("\n\t<option value=\"$day\">$day</option>"); } echo ("\n</select>"); } Quote Link to comment Share on other sites More sharing options...
alphanumetrix Posted April 20, 2009 Share Posted April 20, 2009 This should work, but may not depending on your particular situation. function getDays ($name, $tabIndex) { echo ("\n\n<select name=\"$name\" id=\"$name\" tabindex=\"$tabIndex\">\n <option selected=\"day\" value=\"\">"); $days = array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"); if ($_POST[$name]) $extra = 'selected'; foreach ($days as $day) { echo("\n\t<option value=\"$day\" $extra>$day</option>"); } echo ("\n</select>"); } Quote Link to comment Share on other sites More sharing options...
Axeia Posted April 20, 2009 Share Posted April 20, 2009 function getDays ($name, $tabIndex) { echo "\n\n"."<select name='$name' id='$name' tabindex='$tabIndex'>"; for( $i = 1; $i <= 31; $i++ ) { echo sprintf( "\n\t".'<option %svalue="%s">%s</option>', isset( $_GET[$name] ) && $_GET[$name] == $i ? 'selected="selected" ' : '', $i, $i ); } echo "\n</select>"; } I guess, going by http://www.w3schools.com/TAGS/att_option_selected.asp Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 You can do this with only a couple lines. Define your array at the start of the script and make a function: function makeSelect($arrOptions,$sel,$name) { echo '<select name="'.$name.'">'; foreach ($arrElement as $e) { echo '<option value="'.$e.'"'.($e==$sel ? ' selected' : '').'>'.$e.'</option>'; } echo '</select>'; } Or if you want to use keys: function makeSelectWithKey($arrOptions,$sel,$name) { echo '<select name="'.$name.'">'; foreach ($arrElement as $k => $e) { echo '<option value="'.$k.'"'.($k==$sel ? ' selected' : '').'>'.$e.'</option>'; } echo '</select>'; } $arrOptions: This is your array containg all your data $sel: This is the option selected by the user $name: This is the name of the select $userdays=$_POST['days']; makeSelect($days,$userdays,'days'); Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 20, 2009 Share Posted April 20, 2009 Forgot to mention - you can build an array called $days with the range() function to save typing them all in $days=range(1,31); Quote Link to comment Share on other sites More sharing options...
Axeia Posted April 20, 2009 Share Posted April 20, 2009 Mmmh range() .. didn't know that one btw, in the solution I posted if you're going to use it for differents months you may want to make the the hardcoded 31 an optional parameter, so: function getDays ( $name, $tabIndex, $totDays = 31 /*<- Optional param, defaults to 31 if ommited*/) { echo "\n\n"."<select name='$name' id='$name' tabindex='$tabIndex'>"; for( $i = 1; $i <= $totDays /*<- There it is!*/; $i++ ) { echo sprintf( "\n\t".'<option %svalue="%s">%s</option>', isset( $_GET[$name] ) && $_GET[$name] == $i ? 'selected="selected" ' : '', $i, $i ); } echo "\n</select>"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.