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>';
}
?>

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

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'" : '';

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.