How to make my dropdown sticky

Hi all,

I am trying to make my dropdown sticky. So that when i hit submit and it validates and shows the errors, all my form fields stay. I figured it out for the textboxes. But how do i retain my dropwdown values. Here is some of the code that i using. Let me know if I need to post more.

<tr class="rowcolor1"><td width="162">City:</td>
      <td><input type="text" name="city" class="textbox" maxlength="100" value="<?php if(isset($_POST['City'])) echo $_POST['City']; ?>"></td>
</tr>
<tr class="rowcolor2"><td width="162">State:</td>
      <td><?php
      $result = mysql_query('select * from states order by state_home desc, state');
             ?>
      <select name="stateid">
      <?php
      //Lets loop through the states (only if there are some)
      if(mysql_num_rows($result)==0){}
      else{
      while($row=mysql_fetch_array($result)){
      ?>                                                
      <option value="<?php echo($row['stateid']); ?>"><?php echo($row['state']); ?>
      <?php
      }
      }
      ?>
      </select></td>
      </tr>
stevegingellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

whardemanCommented:
Well, a quick and dirty one might be:

<option value="<?php echo($row['stateid']); ?>" <?php if(isset($_POST['stateid']) && $_POST['stateid']==$row['stateid']) echo 'selected'; ?> ><?php echo($row['state']); ?>

W
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
whardemanCommented:
Another, slightly cleaner way would be:

     while($row=mysql_fetch_array($result)){
        $opt_str .= '<option value="'.$row['stateid'].'">'.$row['state']; //create a string with all your options
     }
     if(isset($_POST['stateid'])){
        $opt_replaced = str_replace('value="'.$_POST['stateid'].'"', 'value="'.$_POST['stateid'].'" selected');
     }
     echo($opt_replaced);

Hope this helps. =)

W
0
stevegingellAuthor Commented:
Works perfectly. Thanks much!
0
whardemanCommented:
Glad to help and good luck! =)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.