kasitzboym Posted December 24, 2011 Share Posted December 24, 2011 Hey guys there is probably a simple fix to this but i am just not seeing it ... Im createing a form that has error catching and self submitting valuse. I am trying to use a for loop to create all of the values and if the form is returned with an error then the correct values will still be filled out but i cant seem to get this loop to work. Every time i submit the form it doesnt matter what option i select it always comes back with the last one in the list or 30 in this case. the problem code is below.. Any help would be appreciated. <?php for($i=1; $i<=30; $i++){ echo'<option value="'.$i.'"'; if(isset($_POST['petsAge'])== $i){ echo ' selected="selected"';} echo '>'.$i.'</option>'; } ?> Link to comment https://forums.phpfreaks.com/topic/253775-for-loop-with-select-tag-always-selects-last-value-option/ Share on other sites More sharing options...
trq Posted December 24, 2011 Share Posted December 24, 2011 isset returns true or false, it will never equal $i. <?php for ($i=1; $i<=30; $i++) { ?> <option value="<?php echo $i; ?>"<?php echo isset($_POST['petsAge']) && $_POST['petsAge'] == $i ? "selected='selected'" : ''; ?>><?php echo $i; ?></option>"; <?php } ?> Link to comment https://forums.phpfreaks.com/topic/253775-for-loop-with-select-tag-always-selects-last-value-option/#findComment-1301005 Share on other sites More sharing options...
kasitzboym Posted December 24, 2011 Author Share Posted December 24, 2011 Quote isset returns true or false, it will never equal $i. WOW looks like 18 hours of codeing a day really has taken its toll on me I totally overlooked that THANKS! Also ive never seen the syntax is that a differnt form of if statement? isset($_POST['petsAge']) && $_POST['petsAge'] == $i ? "selected='selected'" : ''; Link to comment https://forums.phpfreaks.com/topic/253775-for-loop-with-select-tag-always-selects-last-value-option/#findComment-1301007 Share on other sites More sharing options...
trq Posted December 24, 2011 Share Posted December 24, 2011 See a little way down this page: http://au2.php.net/ternary It's the ternary operator. Link to comment https://forums.phpfreaks.com/topic/253775-for-loop-with-select-tag-always-selects-last-value-option/#findComment-1301010 Share on other sites More sharing options...
kasitzboym Posted December 24, 2011 Author Share Posted December 24, 2011 Awesome thanks again! Link to comment https://forums.phpfreaks.com/topic/253775-for-loop-with-select-tag-always-selects-last-value-option/#findComment-1301011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.