Skip to content

Instantly share code, notes, and snippets.

@solepixel
Created August 29, 2015 13:34
Show Gist options
  • Save solepixel/c8c085baca8bf954be82 to your computer and use it in GitHub Desktop.
Save solepixel/c8c085baca8bf954be82 to your computer and use it in GitHub Desktop.
PHP States Select
<?php
<select name="state" id="state">
<option value=""<?php if(!isset($_POST['state']) || $_POST['state'] == ""){ ?> selected="selected"<?php } ?>>Select One</option>
<option value="AL"<?php if($_POST['state'] == "AL"){ ?> selected="selected"<?php } ?>>Alabama</option>
<option value="AK"<?php if($_POST['state'] == "AK"){ ?> selected="selected"<?php } ?>>Alaska</option>
<option value="AZ"<?php if($_POST['state'] == "AZ"){ ?> selected="selected"<?php } ?>>Arizona</option>
<option value="AR"<?php if($_POST['state'] == "AR"){ ?> selected="selected"<?php } ?>>Arkansas</option>
<option value="CA"<?php if($_POST['state'] == "CA"){ ?> selected="selected"<?php } ?>>California</option>
<option value="CO"<?php if($_POST['state'] == "CO"){ ?> selected="selected"<?php } ?>>Colorado</option>
<option value="CT"<?php if($_POST['state'] == "CT"){ ?> selected="selected"<?php } ?>>Connecticut</option>
<option value="DE"<?php if($_POST['state'] == "DE"){ ?> selected="selected"<?php } ?>>Delaware</option>
<option value="DC"<?php if($_POST['state'] == "DC"){ ?> selected="selected"<?php } ?>>District of Columbia</option>
<option value="FL"<?php if($_POST['state'] == "FL"){ ?> selected="selected"<?php } ?>>Florida</option>
<option value="GA"<?php if($_POST['state'] == "GA"){ ?> selected="selected"<?php } ?>>Georgia</option>
<option value="HI"<?php if($_POST['state'] == "HI"){ ?> selected="selected"<?php } ?>>Hawaii</option>
<option value="ID"<?php if($_POST['state'] == "ID"){ ?> selected="selected"<?php } ?>>Idaho</option>
<option value="IL"<?php if($_POST['state'] == "IL"){ ?> selected="selected"<?php } ?>>Illinois</option>
<option value="IN"<?php if($_POST['state'] == "IN"){ ?> selected="selected"<?php } ?>>Indiana</option>
<option value="IA"<?php if($_POST['state'] == "IA"){ ?> selected="selected"<?php } ?>>Iowa</option>
<option value="KS"<?php if($_POST['state'] == "KS"){ ?> selected="selected"<?php } ?>>Kansas</option>
<option value="KY"<?php if($_POST['state'] == "KY"){ ?> selected="selected"<?php } ?>>Kentucky</option>
<option value="LA"<?php if($_POST['state'] == "LA"){ ?> selected="selected"<?php } ?>>Louisiana</option>
<option value="ME"<?php if($_POST['state'] == "ME"){ ?> selected="selected"<?php } ?>>Maine</option>
<option value="MD"<?php if($_POST['state'] == "MD"){ ?> selected="selected"<?php } ?>>Maryland</option>
<option value="MA"<?php if($_POST['state'] == "MA"){ ?> selected="selected"<?php } ?>>Massachusetts</option>
<option value="MI"<?php if($_POST['state'] == "MI"){ ?> selected="selected"<?php } ?>>Michigan</option>
<option value="MN"<?php if($_POST['state'] == "MN"){ ?> selected="selected"<?php } ?>>Minnesota</option>
<option value="MS"<?php if($_POST['state'] == "MS"){ ?> selected="selected"<?php } ?>>Mississippi</option>
<option value="MO"<?php if($_POST['state'] == "MO"){ ?> selected="selected"<?php } ?>>Missouri</option>
<option value="MT"<?php if($_POST['state'] == "MT"){ ?> selected="selected"<?php } ?>>Montana</option>
<option value="NE"<?php if($_POST['state'] == "NE"){ ?> selected="selected"<?php } ?>>Nebraska</option>
<option value="NV"<?php if($_POST['state'] == "NV"){ ?> selected="selected"<?php } ?>>Nevada</option>
<option value="NH"<?php if($_POST['state'] == "NH"){ ?> selected="selected"<?php } ?>>New Hampshire</option>
<option value="NJ"<?php if($_POST['state'] == "NJ"){ ?> selected="selected"<?php } ?>>New Jersey</option>
<option value="NM"<?php if($_POST['state'] == "NM"){ ?> selected="selected"<?php } ?>>New Mexico</option>
<option value="NY"<?php if($_POST['state'] == "NY"){ ?> selected="selected"<?php } ?>>New York</option>
<option value="NC"<?php if($_POST['state'] == "NC"){ ?> selected="selected"<?php } ?>>North Carolina</option>
<option value="ND"<?php if($_POST['state'] == "ND"){ ?> selected="selected"<?php } ?>>North Dakota</option>
<option value="OH"<?php if($_POST['state'] == "OH"){ ?> selected="selected"<?php } ?>>Ohio</option>
<option value="OK"<?php if($_POST['state'] == "OK"){ ?> selected="selected"<?php } ?>>Oklahoma</option>
<option value="OR"<?php if($_POST['state'] == "OR"){ ?> selected="selected"<?php } ?>>Oregon</option>
<option value="PA"<?php if($_POST['state'] == "PA"){ ?> selected="selected"<?php } ?>>Pennsylvania</option>
<option value="RI"<?php if($_POST['state'] == "RI"){ ?> selected="selected"<?php } ?>>Rhode Island</option>
<option value="SC"<?php if($_POST['state'] == "SC"){ ?> selected="selected"<?php } ?>>South Carolina</option>
<option value="SD"<?php if($_POST['state'] == "SD"){ ?> selected="selected"<?php } ?>>South Dakota</option>
<option value="TN"<?php if($_POST['state'] == "TN"){ ?> selected="selected"<?php } ?>>Tennessee</option>
<option value="TX"<?php if($_POST['state'] == "TX"){ ?> selected="selected"<?php } ?>>Texas</option>
<option value="UT"<?php if($_POST['state'] == "UT"){ ?> selected="selected"<?php } ?>>Utah</option>
<option value="VT"<?php if($_POST['state'] == "VT"){ ?> selected="selected"<?php } ?>>Vermont</option>
<option value="VA"<?php if($_POST['state'] == "VA"){ ?> selected="selected"<?php } ?>>Virginia</option>
<option value="WA"<?php if($_POST['state'] == "WA"){ ?> selected="selected"<?php } ?>>Washington</option>
<option value="WV"<?php if($_POST['state'] == "WV"){ ?> selected="selected"<?php } ?>>West Virginia</option>
<option value="WI"<?php if($_POST['state'] == "WI"){ ?> selected="selected"<?php } ?>>Wisconsin</option>
<option value="WY"<?php if($_POST['state'] == "WY"){ ?> selected="selected"<?php } ?>>Wyoming</option>
</select>
<?php
function statesSelect($id, $selected=NULL, $name=NULL, $class=NULL){
$states = array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming'
);
$name = (empty($name)) ? $id : $name;
$return = '<select name="'.$name.'" id="'.$id.'"';
if(!empty($class)){
$return .= ' class="'.$class.'"';
}
$return .= '>';
$return .= '<option value=""';
if(empty($selected)){
$return .= ' selected="selected"';
}
$return .= ">Select State</option>\n";
foreach($states as $k=>$v){
$return .= '<option value="'.$k.'"';
if($selected == $k){
$return .= ' selected="selected"';
}
$return .= ">$v</option>\n";
}
$return .= "</select>\n";
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment