Forst let's build your HTML for the drop-down box (presuming $arrStates contains your list of states)
PHP Code:
$htmStates='';
foreach ($arrStates as $abbrev => $state) {
$htmStates.='<option value="'.$abbrev.'"'.($abbrev==$strState ? ' selected' : '').'>'.$state.'</option>';
}
That will build all your HTML into the variable $htmStates. This also presumes that you've used $_POST to read the state from your form beforehand and uses it as a default value should the form have to be presented to the user again - it remembers what was chosen.
Let's say your form looks like this:
Code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
State: <select name="state"><option value="XX">-- Choose --</option><?php echo $htmStates; ?></select><br>
<input type="submit" name="substates" value="Submit State">
Now, to detect the submit button and that the state is valid we can use this:
PHP Code:
if (isset($_POST['substate'])&&$_POST['state']!='XX') {
header('Location: '.$_POST['state'].'.html.');
exit;
}
Let's say you've chosen Alabama - it'll try and redirect to AL.html
Note the exit after the header() function - this is to make sure the script terminates after redirecting.
Bookmarks