xalu Posted August 24, 2009 Share Posted August 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/171605-solved-selecting-state-then-city-trouble/ Share on other sites More sharing options...
xalu Posted August 24, 2009 Author Share Posted August 24, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/171605-solved-selecting-state-then-city-trouble/#findComment-904925 Share on other sites More sharing options...
kickstart Posted August 24, 2009 Share Posted August 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/171605-solved-selecting-state-then-city-trouble/#findComment-904944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.