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>'; } ?> Quote Link to comment 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 } ?> Quote Link to comment Share on other sites More sharing options...
kasitzboym Posted December 24, 2011 Author Share Posted December 24, 2011 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'" : ''; Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
kasitzboym Posted December 24, 2011 Author Share Posted December 24, 2011 Awesome thanks again! 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.