Jump to content

[SOLVED] Selecting State Then City Trouble


xalu
 Share

Recommended Posts

Hello,

 

I am trying to create a drop down which will select allow a visitor to search through the avaible listings on a website. First the user selects a state, the dropdown is populated by a query which searches the database for Distinct states available. I want it to then use the selected state to populate the second drop down and find all the cities available in that state. This all is one table called lawfirm.

 

Here is some code I have modified from something I found online. An example can be seen here of what it is doing... http://quickfirms.com.

 

<form name=form action=<?php echo $_SERVER['PHP_SELF']; ?> method=post>
<?php
$dbc = mysqli_connect("localhost","p24tnugh_lawfirm","password","p24tnugh_lawfirm")
or die ("Unable to connect to the database");
//
$query = "SELECT DISTINCT state, state as name FROM lawfirm";
$result = mysqli_query($dbc, $query);
//State Selection
echo "<select DISTINCT name=state onchange='document.form.submit();'>";
if (!isset($_POST['state']))
{ echo '<option value=null selected>Choose a State</option>';}
else
{
echo '<option value=' . $_POST['state'] . '>' . $_POST['state'] . '</option>'; }
while($row = mysqli_fetch_array($result))
{
echo '<option value=' . $row['state'];

if ($row['state'] == $state) { echo ' selected'; }
echo '>' . $row['name'] . '</option>';
}
echo '</select> ';
// County Selection
echo "<select name=city='document.form.submit();'>";

if (!isset($_POST['state']))
{ echo '<option value=null selected>Choose a State First</option>'; }
else
{
$query2="select city, city as name from lawfirm where state = $state";
$result2 = mysqli_query($dbc, $query2);
echo '<option value=' . $_POST['city'] . '>' . $_POST['city'] . '</option>';
echo '<option value=null selected>Choose a City</option>'; }

while($row = mysqli_fetch_array($result2))
{
echo '<option value=' . $row['city'];
if ($row['city'] == $county) { echo ' selected'; }
echo '>' . $row['name'] . '</option>';
}
echo '</select> ';

?>
<input type="submit" value="Search" />
</form>

 

I would appreciate any help

 

MySQL client version: 5.0.67

CREATE TABLE `lawfirm` (

`id` int(10) unsigned NOT NULL auto_increment,

`name` varchar(99) character set utf8 collate utf8_unicode_ci NOT NULL,

`specialty` varchar(99) character set utf8 collate utf8_unicode_ci NOT NULL,

`city` varchar(99) character set utf8 collate utf8_unicode_ci NOT NULL,

`email` varchar(99) character set utf8 collate utf8_unicode_ci NOT NULL,

`website` varchar(99) character set utf8 collate utf8_unicode_ci NOT NULL,

`description` varchar(9999) character set utf8 collate utf8_unicode_ci NOT NULL,

`image` varchar(99) character set utf8 collate utf8_unicode_ci NOT NULL,

`phone` varchar(20) character set utf8 collate utf8_unicode_ci NOT NULL,

`state` varchar(30) character set utf8 collate utf8_unicode_ci NOT NULL,

PRIMARY KEY  (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

Link to comment
Share on other sites

I found a couple errors above and corrected them

 

I attempted to fill in the $state with a state...and I received all cities in that state. So I believe the state isn't being passed into the second query

 

<form name=form action=<?php echo $_SERVER['PHP_SELF']; ?> method=post>
<?php
$dbc = mysqli_connect("localhost","p24tnugh_lawfirm","password","p24tnugh_lawfirm")
or die ("Unable to connect to the database");
//
$query = "SELECT DISTINCT state, state as name FROM lawfirm";
$result = mysqli_query($dbc, $query);
//State Selection
echo "<select DISTINCT name=state onchange='document.form.submit();'>";
if (!isset($_POST['state']))
{ echo '<option value=null selected>Choose a State</option>';}
else
{
echo '<option value=' . $_POST['state'] . '>' . $_POST['state'] . '</option>'; }
while($row = mysqli_fetch_array($result))
{
echo '<option value=' . $row['state'];

if ($row['state'] == $state) { echo ' selected'; }
echo '>' . $row['name'] . '</option>';
}
echo '</select> ';

// City Selection
echo "<select name=city onchange='document.form.submit();'>";

if (!isset($_POST['state']))
{ echo '<option value=null selected>Choose a State First</option>'; }
else
{
$query2="SELECT city, city AS name FROM lawfirm 
WHERE state = '$state' ";
$result2 = mysqli_query($dbc, $query2);
echo '<option value=' . $_POST['city'] . '>' . $_POST['city'] . '</option>';
echo '<option value=null selected>Choose a City</option>'; }

while($row = mysqli_fetch_array($result2))
{
echo '<option value=' . $row['city'];
if ($row['city'] == $county) { echo ' selected'; }
echo '>' . $row['name'] . '</option>';
}
echo '</select> ';

?>
<input type="submit" value="Search" />
</form>

Link to comment
Share on other sites

Hi

 

Bit of a play. You didn't appear to be setting $state anywhere.

 

<form name=form action=<?php echo $_SERVER['PHP_SELF']; ?> method=post>
<?php
$state = $_POST['state'];
$dbc = mysqli_connect("localhost","p24tnugh_lawfirm","password","p24tnugh_lawfirm") or die ("Unable to connect to the database");
//
$query = "SELECT DISTINCT state, state as name FROM lawfirm";
$result = mysqli_query($dbc, $query);
//State Selection
echo "<select name='state' onchange='document.form.submit();'>";
if (!isset($_POST['state']))
{ 
echo '<option value=null selected>Choose a State</option>';
}
else
{
echo '<option value=' . $_POST['state'] . '>' . $_POST['state'] . '</option>'; 
}
while($row = mysqli_fetch_array($result))
{
echo "<option value='".$row['state']."' ".(($row['state'] == $state) ? ' selected' : '' )." >" . $row['name'] . '</option>';
}
echo '</select> ';

// City Selection
echo "<select name='city' onchange='document.form.submit();'>";

if (!isset($_POST['state']))
{ 
echo '<option value=null selected>Choose a State First</option>'; 
}
else
{
$query2="SELECT DISTINCT city, city AS name FROM lawfirm 
WHERE state = '$state' ";
$result2 = mysqli_query($dbc, $query2);
echo '<option value=' . $_POST['city'] . '>' . $_POST['city'] . '</option>';
echo '<option value=null selected>Choose a City</option>'; 
while($row = mysqli_fetch_array($result2))
{
	echo "<option value='" . $row['city']."' ".(($row['city'] == $county) ? ' selected' : '' ).">" . $row['name'] . '</option>';
}
}
echo '</select> ';
?>
<input type="submit" value="Search" />
</form>

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.