[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to make my dropdown sticky

Posted on 2006-04-07
4
Medium Priority
?
271 Views
Last Modified: 2013-12-12
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>
0
Comment
Question by:stevegingell
  • 3
4 Comments
 
LVL 2

Accepted Solution

by:
whardeman earned 1000 total points
ID: 16403404
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
 
LVL 2

Expert Comment

by:whardeman
ID: 16403514
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
 

Author Comment

by:stevegingell
ID: 16403686
Works perfectly. Thanks much!
0
 
LVL 2

Expert Comment

by:whardeman
ID: 16403720
Glad to help and good luck! =)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question