tefuzz Posted April 20, 2009 Share Posted April 20, 2009 Ok, so i created a function for my form to populate a list with years for use with birth dates. I added more functionality to it so that when validating and error checking, I could have the function still populate the list, but if a year was selected and there were other errors, not pertaining to the list it would still keep that year selected after reload. my function takes a start/end year and populates the list accordingly from highest to lowest, and checks to see which value is selected...That's where I cant figure out why it actually works. Anwyho, so here is how it is right now. Works flawlessly (might not be the best way, but im a beginner, and it works ) function getYears($name, $startYear, $endYear, $tabIndex, $selected) { $year = $endYear; echo ("\n\n<select name=\"$name\" id=\"$name\" tabindex=\"$tabIndex\">\n"); if(empty($selected)){ echo("\n\t<option selected=\"selected\" value=\"\"></option>"); } while ($year >= $startYear) { if ($year == $selected) { echo("\n\t<option selected=\"selected\" value=\"$selected\">$selected</option>"); } else { echo("\n\t<option value=\"$year\">$year</option>"); } $year --; } echo ("\n</select>"); } With it like that, it works 100% no problems, none ever...However, while trying to figure it out, I had coded it a little differently, and it would populate the list with only 2 values, both being blank. here is the only difference in code: echo("\n\t<option selected=\"selected\" value=\"$year\">$year</option>"); instead of echo("\n\t<option selected=\"selected\" value=\"$selected\">$selected</option>"); Considering the statement below, shouldnt $year AND $selected work in such a case since they are equal? if ($year == $selected) { Quote Link to comment Share on other sites More sharing options...
alphanumetrix Posted April 20, 2009 Share Posted April 20, 2009 Um... I don't think selected is supposed to be used in that way. I think it's supposed to be like: <option value="something" selected>Something</option> Try your function like this instead: function getYears($name, $startYear, $endYear, $tabIndex, $selected) { $year = $endYear; echo ("\n\n<select name=\"$name\" id=\"$name\" tabindex=\"$tabIndex\">\n"); if(empty($selected)){ echo("\n\t<option selected=\"selected\" value=\"\"></option>"); } while ($year >= $startYear) { if ($year == $selected) { echo("\n\t<option value=\"$year\" selected>$year</option>"); } else { echo("\n\t<option value=\"$year\">$year</option>"); } $year --; } echo ("\n</select>"); } Yes, because $year == $selected, it should be the same. However, == can mean equivalent values, too. So having said that, if $year = 0; & $selected = false; - I believe that would have the same translation. So instead of using this operator, perhaps try the identical comparison operator: === (just an extra =) Quote Link to comment Share on other sites More sharing options...
tefuzz Posted April 20, 2009 Author Share Posted April 20, 2009 Um... I don't think selected is supposed to be used in that way. I think it's supposed to be like: <option value="something" selected>Something</option> Yes, because $year == $selected, it should be the same. However, == can mean equivalent values, too. So having said that, if $year = 0; & $selected = false; - I believe that would have the same translation. So instead of using this operator, perhaps try the identical comparison operator: === (just an extra =) I have always used selected="selected" and it is validating according to the w3c...as for the === thanks, makes more sense to me. 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.