Jump to content

for loop with select tag always selects last value option


kasitzboym

Recommended Posts

Hey guys there is probably a simple fix to this but i am just not seeing it  :shrug: ... 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
Share on other sites

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
Share on other sites

isset returns true or false, it will never equal $i.

 

:o 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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.