Luodeni Posted March 7, 2009 Share Posted March 7, 2009 I created a function to dynamically create a list of date available which will later be checked with checkdate(). Now the funny thing is that i was able to echo the result back from the user but it just created a new <option> which isn't very neat. Therefore I'd like to ask whether anyone knows how to do this neatly, that means selecting the value from the already generated list. many thanks in advance here is the code <?php function createMonths() { for ( $iMonth = 1; $iMonth <= 12; $iMonth++ ) { echo "<option>" . $iMonth . "</option>"; } return $iMonth; } function validDateCheck( $dobMonth, $dobDay, $dobYear ) { if (checkdate( $dobMonth, $dobDay, $dobYear) == true ) { $message .= ""; } else { $message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " Is not a valid date, please correct it! <br />"; } return $message; } ?> <select name="dateofbirthmonth" tabindex="7" class="dropdownStyle" style="width: 37px; "> <?php if ( $_POST['dateofbirthmonth'] != "") { echo "<option value=" . $_POST['dateofbirthmonth'] . " selected>" . $_POST['dateofbirthmonth'] . "</option>"; } else { echo "<option value='00' selected>M</option>"; } ?> <?php createMonths(); ?> </select> Link to comment https://forums.phpfreaks.com/topic/148341-solved-returning-a-dropdown-box-value-in-my-function/ Share on other sites More sharing options...
wildteen88 Posted March 7, 2009 Share Posted March 7, 2009 I'd move this if ( $_POST['dateofbirthmonth'] != "") { echo "<option value=" . $_POST['dateofbirthmonth'] . " selected>" . $_POST['dateofbirthmonth'] . "</option>"; } else { echo "<option value='00' selected>M</option>"; } To within your createMonths function function createMonths() { for ( $iMonth = 1; $iMonth <= 12; $iMonth++ ) { $selected = (isset($_POST['dateofbirthmonth']) && $_POST['dateofbirthmonth'] == $iMonth) ? ' selected="selected"' : null ; echo "<option{$selected}>" . $iMonth . "</option>"; } return $iMonth; } Link to comment https://forums.phpfreaks.com/topic/148341-solved-returning-a-dropdown-box-value-in-my-function/#findComment-778810 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.