Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple listbox issue

Status
Not open for further replies.

ehicks727

Programmer
Oct 26, 2004
36
US
I'm having an issue with the following code snippet. What I'm trying to do is append a list of states together when multiples are selected. So, if they select Alabama, it should just return AL. If they select Alabama and Georgia, then it should display AL,GA. (note the comma in between states).

The problem I'm having is that if a user hits their back button and then SUBMIT again, it concatenates everything. So, for the first time it will display AL,GA,FL. If they hit back and SUBMIT again, it'll display AL,GA,FLAL,GA,FL

How can I prevent this from happening? Thanks.

Code:
if (empty($_POST['submit']) ) {
         echo '<form name="test" action="test.php" method="POST">
                     <select multiple name="opstates[]" size="3">
                       <option value="AL" id="AL">Alabama</option>
                       <option value="FL" id="FL">Florida</option>
                       <option value="GA" id="GA">Georgia</option>
                     </select><input type="submit" name="submit" value="Submit"></form>';

     } else {
         extract($_REQUEST);
         $s = "";
         foreach ($opstates as $v) {
             if (strlen($s) > 0) { $s = $s.','; }
             $s = $s.$v;
             echo $s;
         }
     }
 
I just moved my echo $s; out of the foreach loop and it works fine. sheesh... it's been one of those days.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top