I am new to all of this and I’m totally lost.
:confused2
I have the following ‘Customer’ table in a MySQL database:
CustomerID
CustomerName
Address
City
StateProv
PostalCode
Country
There are numerous customers and I need three drop down
lists that allow the user to select a Country then based
on that country, the next drop down list will list the
states for that country. After a state is selected the
third drop down box will contain a list of the cities
for that state. There will be a button at the bottom of
the lists (Display List) this sends these choices to a
new page that displays only the customers for the country,
state and city chosen.
I have been trying to avoid using JavaScript, so I have
come up with the following code. The problem is I get the
list of countries, but the information is not passed to the
selectCountry variable, so nothing works.
Can anyone tell me what I’m missing?
:bawling:
<?php
include("db.php");
mysql_select_db($dbname);
if (isset($HTTP_POST_VARS['clear'])) {
unset($HTTP_POST_VARS['selectCity']);
unset($HTTP_POST_VARS['selectState']);
unset($HTTP_POST_VARS['selectCountry']);
}
if (!isset($HTTP_POST_VARS['selectCountry'])) {
$selectCountry = "";
}
if (!isset($HTTP_POST_VARS['selectState'])) {
$selectState = "";
}
if (!isset($HTTP_POST_VARS['selectCity'])) {
$selectCity = "";
}
$cityList = array();
$stateList = array();
$countryList = array();
$get_countries = mysql_query("SELECT DISTINCT (Country) FROM `Customer` ORDER BY Country ASC ");
if (!$get_countries) {
print "get_countries<br>";
print mysql_error() . "<br><br>";
}
if (isset($HTTP_POST_VARS['selectCountry']))
{
$selectCountry = $HTTP_POST_VARS['selectCountry'];
$get_states = mysql_query("SELECT DISTINCT (StateProv) FROM Customer WHERE Customer.Country = '$selectCountry' ORDER BY StateProv ASC");
if (!$get_states) {
print "get_state<br>";
print mysql_error() . "<br><br>";
}
}
if (isset($HTTP_POST_VARS['selectCity'])) {
$selectState = $HTTP_POST_VARS['selectState'];
$selectCity = $HTTP_POST_VARS['selectCity'];
$get_cities = mysql_query("
SELECT DISTINCT (City) FROM Customer WHERE Customer.StateProv = '$selectState' AND Customer.Country = '$selectCountry'");
if (!$get_cities) {
print "get_cities<br>";
print mysql_error() . "<br><br>";
}
}
if (@mysql_num_rows($get_countries)!=0) {
while ($item = mysql_fetch_array($get_countries)) {
if ($item["selectCountry"] == $selectCountry) {$sel = " selected";} else {$sel = "";}
array_push ($countryList,"<option value=\\"" . $item["selectCountry"] . "\\" " . $sel . ">" . $item["Country"] . "</option>");
}
}
else {
array_push ($countryList,"<option>---------</option>");
}
if (@mysql_num_rows($get_states)!=0) {
while ($item = mysql_fetch_array($get_states)) {
if ($item["State"] == $selectState) {$sel = " selected";} else {$sel = "";}
array_push ($cityList,"<option value=\\"" . $item["selectState"] . "\\" " . $sel . ">" . $item["State"] . "</option>");
}
}
else {
array_push ($cityList,"<option>---------</option>");
}
if (@mysql_num_rows($get_cities)!=0) {
while ($item = mysql_fetch_array($get_cities)) {
if ($item["City"] == $selectCity) {$sel = " selected";} else {$sel = "";}
array_push ($stateList,"<option value=\\"" . $item["selectCity"] . "\\" " . $sel . ">" . $item["City"] . "</option>");
}
}
else {
array_push ($stateList,"<option>---------</option>");
}
?>
<html>
<head>
<title>Directory</title><meta name="Microsoft Border" content="b, default"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table border="1" width="950" cellpadding="5">
<tr>
<td width="150" valign="top" bgcolor="#33CCFF">
<?
include('mainMenu.inc');
?>
</td>
<td valign="top" >
<?
include('chooseHeader.inc');
?>
<table width="800" cellpadding="5">
<p align="left"><font size="3" face="Verdana, Arial, Helvetica, sans-serif">
Select the name of the country, press 'SELECT'. A list of states will appear,
Select the name of the state, press 'SELECT'. A list of cities will appear,
Select the name of the city, press 'SELECT'.
Use the 'DISPLAY LIST' button to see the cities listed the chosen state.</font><font size="3" face="Verdana, Arial, Helvetica, sans-serif">
</font></p>
<?php
if (isset($_SERVER)) $PHP_SELF = $_SERVER['PHP_SELF'];
?>
<FORM ACTION="<?php print $PHP_SELF; ?>" METHOD="POST">
<input type="hidden" name="selectCountry" value="<?php print @$selectCountry; ?>">
<input type="hidden" name="selectState" value="<?php print @$selectState; ?>">
<input type="hidden" name="selectCity" value="<?php print @$selectCity; ?>">
<tr><td>
<font face="Verdana, Arial, Helvetica, sans-serif" size="4"> <font size="4">Choose a Country:
<select name="selectCountry">
<?php
foreach ($countryList as $row) {
print $row;
}
?>
</select> </font>
</tr></td>
<tr><td>
<font face="Verdana, Arial, Helvetica, sans-serif" size="4">Choose a State:
<select name="selectState">
<?php
foreach ($cityList as $row) {
print $row;
}
?>
</select> </font>
</td></tr>
<tr><td>
<font face="Verdana, Arial, Helvetica, sans-serif" size="4">Choose a City:
<select name="selectCity">
<?php
foreach ($stateList as $row) {
print $row;
}
?>
</select> </font>
</td></tr>
<tr><td>
<input type="submit" value="NEXT" name="next">
<input type="submit" value="CLEAR" name="clear">
</td></tr>
</form>
<tr><td>
<FORM name="passData" method=post action="displayList.php">
<div align="center">
<center>
<p><span style="font-family: Verdana, Arial">
<input type="submit" value="Display List" >
</span></p>
</center>
</div>
</form>
</td></tr>
<br>
</td>
<tr>
<td>
<?php
include('mainFooter.inc');
?>
</td>
</tr>
</table>
</body>