Posted By

aeboi80 on 07/06/10


state drop down province remember

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


PHP Dynamic Dropdown Box: State & Province - Remember Value On Submit

 / Published in: PHP

This snippet will allow you to dynamically create a form dropdown list of all of the states within the US, Canada and UK. It pulls them from a table called state in a database. This snippet is used in conjunction with which will actually create the state table within your database. The dropdown list is sorted by the state_id so all US states are listed together, all CA provinces are listed together and the same for the UK.

  1. $sql = "select state_id,state_name,state_abbr from state order by state_id";
  2. if (!(@ $result = mysql_query($sql))) {
  3. print "There was an error running your query:<p>\n". $sql . "<p>\n";
  4. }
  5. print '<select name="state"><br>\n';
  6. print '<option value=""> - Select State/Province - ';
  7. while ($row = mysql_fetch_row($result)) {
  8. $state_id = $row[0];
  9. $state_name = $row[1];
  10. $state_abbr = $row[2];
  12. print "<option value=\"$state_abbr\"";
  13. if ($_REQUEST["state"] == $state_abbr) { print " selected"; }
  14. print "> $state_name <br>\n";
  15. }
  16. print '</select>';

Report this snippet  

You need to login to post a comment.