Good Morning All,

Hopefully someone out there has a resolution to this issue.

I have a form that I want to use drop down boxes for the country and state.

That is no problem, I already have them.

The issue comes in when someone has filled out the form, but there is an error in their entry, so they have to be taken back to the form to correct the error.

I do this by creating a secondary form with all the same fields in it, in hidden format, and then submit that form, which carries all the data they selected back into the original form.

With ONE (two) EXCEPTION.. the drop down boxes. They revert to no selection.

What is the simple solution to this? Any suggestions?

<select id="state" name="state">
  <option value="">Select State</option>
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
...etc
</select>

How do I carry what they have selected on their first attempt back so it will display whichever state they chose?

Thanks in advance.

Douglas

Recommended Answers

All 5 Replies

Assuming you are retrieving your countries from a mysql table. Then your code should be something like the one below. ie. Loop for all the countries and check if the posted country value matches the one in the current loop. If so, display it as selected.

$postedCountry = isset($_REQUEST['country_id']) ? $_REQUEST['country_id'] : 0;
$rResult = mysql_query("select * from tblCountries");
$sOptions = '';
while($aCountry = mysql_fetch_assoc($rResult))
{
  if($aCountry['country_id'] == $postedCountry)
    $sSelected = 'selected="selected"';
  else
    $sSelected = '';

  //make the country options 
  $sOptions .= "<option value='$aCountry[country_id]' $sSelected > $aCountry[country_name]</option>";
}

echo "<select name='country_id'>$sOptions</select>";

As for the states, you can do the same, but applying the respective field and table names

WOW... There is just something about posting a question in here that makes my mind work in a different fashion apparently.

Within a few minutes of posting this question, an idea popped into my head, and I tried it, AND IT WORKED...

Still have another question on how to make the process simpler. I'm sure it has to do with an array or something along those lines, but implementing it is the question.

Here is what I did to resolve the posted issue.

<select id="state" name="state">
  <option value="">Select State</option>
  <option value="AL" <?php if ($state=='AL'){print" selected=\"selected\"";}?>>Alabama</option>
  <option value="AK" <?php if ($state=='AK'){print" selected=\"selected\"";}?>>Alaska</option>
... etc

Then when I passed back the variable $state the correct state was displayed.

NOW, I can do this by going through the entire state list and adding this print statement to each line, and also to the country list (very long list), but I know that there must be an easier way to accomplish this.

Any help would be greatly appreciated.

Douglas

OK Wilch, thanks for the reply. I think you came close to answering my second question as well as the first, but just to clarify:

My state and country lists are simply select/option lists saved as .php files and included into the script when it runs.

<select id="state" name="state">
  <option value="">Select State</option>
  <option value="AL" <?php if ($state=='AL'){print" selected=\"selected\"";}?>>Alabama</option>
  <option value="AK" <?php if ($state=='AK'){print" selected=\"selected\"";}?>>Alaska</option>

  <option value="AZ" <?php if ($state=='AZ'){print" selected=\"selected\"";}?>>Arizona</option>
  <option value="AR" <?php if ($state=='AR'){print" selected=\"selected\"";}?>>Arkansas</option>
  <option value="CA" <?php if ($state=='CA'){print" selected=\"selected\"";}?>>California</option>
  <option value="CO" <?php if ($state=='CO'){print" selected=\"selected\"";}?>>Colorado</option>
</select>

is there a simpler way to manage this?.. i.e. database?

Yes there is. If you can make use of a database, then import the 2 lists into 2 separate tables - tblCountries and tblStates. Then try to incorporate the idea of the code i sent earlier on.

OK Great... it is working fine.

I had to tweak it a bit to fit in with what I had, but nothing serious.

the variable I had feeding back with the currently selected country was $ccode, so I just had to plug that into the following snippet as provided above:

$rResult = mysql_query("select * from countries");
$sOptions = '';
while($aCountry = mysql_fetch_assoc($rResult)){
    if($aCountry['ccode'] == $ccode){
        $sSelected = 'selected="selected"';
    }else {
        $sSelected = '';
    }
//make the country options
$sOptions .= "<option value='$aCountry[ccode]' $sSelected > $aCountry[country]</option>";
}
print "<select name='ccode'>$sOptions</select>";

And, it works perfectly. Thank you very much.

Of course I did need to create and populate the tables in the database, but that was pretty easy too.

Thanks again for your direction.
Marking this as solved...

Douglas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.