Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created August 30, 2013 10:21
Show Gist options
  • Save phpfiddle/6388445 to your computer and use it in GitHub Desktop.
Save phpfiddle/6388445 to your computer and use it in GitHub Desktop.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("testing");
?>
<form action="" method="post">
<select name="country">
<?php
$country_query = "SELECT country_id, country_name FROM countries";
$country_result = mysql_query($country_query);
while($country = mysql_fetch_array($country_result)) {
if($_POST['country'] == $country['country_id'] ) {
echo '<option selected value="' . $country['country_id'] . '">' . $country['country_name'] . '</option>';
} else {
echo '<option value="' . $country['country_id'] . '">' . $country['country_name'] . '</option>';
}
}
?>
</select>
<input type="submit" name="submit" value="GO" /><br /><br />
<select name="city">
<?php
if($_POST['submit']) {
echo $_POST['country'];
$city_query = "SELECT city_id, city_name FROM cities WHERE country_id = '{$_POST['country']}'";
$city_result = mysql_query($city_query);
while($city = mysql_fetch_array($city_result)) {
echo '<option value="' . $city['city_id'] . '">' . $city['city_name'] . '</option>';
}
}
?>
</select>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment