Guys,
I've been playing with this for days, and it's time to ask the experts.
I'm pulling the list for a drop-down box from a database. Then using POST to make a new request from the database, which populates a new drop-down list.
The table has 3 fields, an ID (locID), State, City.
Here is code that functions, but does not give me the results I want:
CODE
function FindLoc($locID)
{
// Check to see if State has been Selected
if (!isset($_POST[State]))
{
// find the state
$result = mysql_query("SELECT DISTINCT State FROM Location ORDER BY State ")or die(mysql_error());
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
<p>Select a State </p>
<select name='State' value=''>
<?php
while($row = mysql_fetch_array($result))
{
echo "<option value = $row[State] >$row[State]</option>";
}
?>
<input type="submit" value="Submit" name="Submit">
</select>
</form>
<?php
}
else
{
// find the city
$result = mysql_query("SELECT DISTINCT City,locID FROM Location WHERE State LIKE '%$_POST[State]%' ORDER BY City ")or die(mysql_error());
?>
<form method="post" action="CityPage.php">
<p>Select a City </p>
<select name='City' value=''>
<?php
while($row = mysql_fetch_array($result))
{
echo "<option value = $row[locID] >$row[City]</option>";
}
?>
<input type="Submit" value="Get City" name="Get City">
</select>
</form>
<?php
$locID = $_POST["City"];
return $locID;
}
}
Originally, I put
CODE
$result = mysql_query("SELECT DISTINCT City,locID FROM Location WHERE State = '$_POST[State]' ORDER BY City ")or die(mysql_error());
With the LIKE, After the first Query, it works great, unless I have a state with 2 names (NEW YORK).
Without the LIKE, it works, but POST only contains the first word of the state. (so Utah functions exactly as expected, with New York, POST only contains New)
I put in LIKE to continue testing.
The problem with that is I need an exact match on State, because at this point, if I choose New York, I get all cities in any state which has "New" in the name -- and they are going to notice if I say Newark is in New York!
At any other point in the program, I can use $_POST[State], and the full state shows up.
This same reaction happens if I post the form to itself or to the new page.
I know I can str_replace, but I want to keep the spaces so it is readable for the menus.