FYI... AJAX will do the same without resubmitting the form.. It does, however, require that you have 3 files... You main Form page(php), a Javascript file, and a 2nd PHP page that executes just the SQL Query you want...
So...
PHP Form
<html>
<head>
<title></title>
<script src='get_city.js'></script>
</head>
<body>
<form name='form' id='form' action='action_page.php' method='POST'>
<div><label for="State">State : </label>
<select onchange="get_city(this.value)" name="State" id="State">
<option value="COLORADO">COLORADO</option>
</select>
</div>
<div>
<label for="City">City : </label>
<select name="City" id="City">
<option value='Select One'>Select One</option>
</select>
</div>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Javascript file named get_city.js (in same directory)
var xmlHttp
function get_city(state)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert("Your browser does not support AJAX!");
return false;
}
var cityList = document.getElementById("City");
var optionItem;
if (state.length==0)
{
// Clear existing list
for (var count= cityList.options.length=1; count >-1; count--)
{
cityList.options[count] = null;
}
textValue = "Select One";
optionItem = new Option(textValue, textValue, false, false);
cityList.options[0] = optionItem;
return false;
}
var url='get_city.php'; // your php SQL file
url=url+"?q="+state;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,false);
xmlHttp.send(null);
var result = xmlHttp.responseText;
var cities = result.split(","); // you'll be returning your values in a csv value
var size = cities.length;
//clear existing values
for (var count= cityList.options.length=1; count >-1; count--)
{
cityList.options[count] = null;
}
// create new list
for (var count = 0; count<size; count++)
{
textValue = cities[count];
optionItem = new Option(textValue, textValue, false, false);
cityList.options[cityList.length] = optionItem;
}
cityList.options[0].selected = true; // sets the first value as selected.
}
// Watches for change in state of "State" field
function stateChanged()
{
if(xmlHttp.readyState==4)
{
document.getElementById("City").innerHTML=xmlHttp.responseText;
}
}
// Tests for AJAX Compatibility of browsers.
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
And then your simple PHP SQL file named get_city.php
<?php
$q = $_GET['q'];
// Open your database here.....
$host = "host name";
$user = "username";
$password = "password";
$db = mysql_connect($host, $user, $password);
$database = "database name";
mysql_select_db($database, $db);
$strSQL = "SELECT city FROM table_name WHERE state='" . $q . "' ORDER BY city";
$result = mysql_query($strSQL);
$rows = mysql_num_rows($result);
$counter=0;
while ($row = mysql_fetch_assoc($result)) {
if($counter==0) {
$html = $row['city'];
} else {
$html .= "," . $row['city'];
}
$counter++;
}
echo $html;
?>
These will all be small file sizes, but unless I made a typo here (I'm at work so I can't test it) it should work fine. You'll just need to tweak it with your database & field info..
Good Luck