Hi,
I have registration form that contains country and state drop down fields. I use PHP to access a MySQL database to bring up a list of countries for the country drop down field. However, I only want to display a state drop down field if a country with states such as the United States is selected. Then if the state field is displayed, I want it to bring up the relevant states to form the drop down list. I have already asked this question in the PHP area but was told that it sounded more like a javascript question.
So far, I have it where PHP will bring up a list of countries then when a country is selected, javascript will pop up an alert with the name of the country selected. I'm not sure what the next step is to get the corresponding country's states to show up if a country with states is selected.
Here is my code so far:
<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
function DisplayStatesOrCities(member_country)
{
alert(member_country.value)
}
//-->
</script>
</head>
<body>
<p align="center">
<form method="POST" action=" PROCESSING URL">
Country:
<select name="member_country" onchange="DisplayStatesOrCities(this)">
<option>
</option>
<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link)
{
die('Not connected: ' . mysql_error());
}
// MAKE DB THE CURRENT DB
$db_selected = mysql_select_db('db', $link);
if (!$db_selected)
{
die('Can\'t switch to database: ' . mysql_error());
}
$query = "SELECT DISTINCT LOCATION_COUNTRY FROM LOCATION";
$result = mysql_query($query);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($result))
{
$country = stripslashes($row["LOCATION_COUNTRY"]);
print "<option value=\"$country\">" . $country . "</option>";
}
mysql_free_result($result);
?>
</select><br>
State:
<input type="text" name="member_state" size="20"><br>
City:
<input type="text" name="member_city" size="20"><br>
</form>
</p>
</body>
</html>
Thanks!
Ben
I have registration form that contains country and state drop down fields. I use PHP to access a MySQL database to bring up a list of countries for the country drop down field. However, I only want to display a state drop down field if a country with states such as the United States is selected. Then if the state field is displayed, I want it to bring up the relevant states to form the drop down list. I have already asked this question in the PHP area but was told that it sounded more like a javascript question.
So far, I have it where PHP will bring up a list of countries then when a country is selected, javascript will pop up an alert with the name of the country selected. I'm not sure what the next step is to get the corresponding country's states to show up if a country with states is selected.
Here is my code so far:
<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
function DisplayStatesOrCities(member_country)
{
alert(member_country.value)
}
//-->
</script>
</head>
<body>
<p align="center">
<form method="POST" action=" PROCESSING URL">
Country:
<select name="member_country" onchange="DisplayStatesOrCities(this)">
<option>
</option>
<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link)
{
die('Not connected: ' . mysql_error());
}
// MAKE DB THE CURRENT DB
$db_selected = mysql_select_db('db', $link);
if (!$db_selected)
{
die('Can\'t switch to database: ' . mysql_error());
}
$query = "SELECT DISTINCT LOCATION_COUNTRY FROM LOCATION";
$result = mysql_query($query);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($result))
{
$country = stripslashes($row["LOCATION_COUNTRY"]);
print "<option value=\"$country\">" . $country . "</option>";
}
mysql_free_result($result);
?>
</select><br>
State:
<input type="text" name="member_state" size="20"><br>
City:
<input type="text" name="member_city" size="20"><br>
</form>
</p>
</body>
</html>
Thanks!
Ben