bugzy Posted August 9, 2012 Share Posted August 9, 2012 Hello guys.. I have this function <?php function me_date_dropdown($year_limit = 0) { $html_output = ' <select name="date_year" id="year_select">'."\n"; for ($year = 1940; $year <= (date("Y") - $year_limit); $year++) { $html_output .= " <option selected="; if(isset($_POST['submit'])) { if($_POST['date_year'] == $year) { $html_output .= "\"selected\""; } } $html_output .= ">". $year . '</option>'."\n"; } $html_output .= ' </select>'."\n"; return $html_output; } ?> If the user wasn't able to pass the validation on the form registration, I want to pass the previous year he set during the submission. Problem is, for example I chose year "1985", it's always passing me the latest year which is "2012". I don't know if there's a problem on the concatenation or on the if station.. Anyone? Link to comment https://forums.phpfreaks.com/topic/266850-problem-with-if-concatenation/ Share on other sites More sharing options...
PeoMachine Posted August 9, 2012 Share Posted August 9, 2012 change this: $html_output .= "<option selected="; if(isset($_POST['submit'])) { if($_POST['date_year'] == $year) { $html_output .= "\"selected\""; } } $html_output .= ">". $year . '</option>'."\n"; for this: $html_output .= "<option "; if(isset($_POST['submit'])) { if($_POST['date_year'] == $year) { $html_output .= "selected=\"selected\""; } } $html_output .= ">". $year . '</option>'."\n"; Just print "selected=" if you want the option active. You are printing "selected" on all of them. And you can write this way too: $selected = ''; if(isset($_POST['submit']) && ($_POST['date_year'] == $year)) $selected = 'selected="selected"'; $html_output .= "<option $selected>$year</option>\n"; Link to comment https://forums.phpfreaks.com/topic/266850-problem-with-if-concatenation/#findComment-1368063 Share on other sites More sharing options...
bugzy Posted August 9, 2012 Author Share Posted August 9, 2012 Thanks! Solved! Link to comment https://forums.phpfreaks.com/topic/266850-problem-with-if-concatenation/#findComment-1368081 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.