Moderators: macek, egami, gesf







<input type="submit" value="' . (!(isset($_POST['country']))?'Get cities':'Submit form') . '" />';










<?php
$db = new PDO("mysql:host=localhost;dbname=test", "root", "");
if ($_POST) {//are we responding to a form?
$country = $_POST['country'];
$sql_city = "SELECT * FROM city WHERE country_id =" . $country;
$cityResult = $db->query($sql_city);
if ((!$cityResult) || $cityResult->rowCount()) {
die('could not pull city names for country ' . $country);
}
}
$sql_country = "SELECT * FROM country";
$countryResult = $db->query($sql_country);
if ((!$countryResult) || $countryResult->rowCount() == 0) {
die('no countries pulled from database');
}
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Country List</title>
</head>
<body>';
echo '<form id="frm" action="' . ((isset($_POST['city'])?'fullform.php':$_SERVER['PHP_SELF'])) . '" method="post">';
echo '<select name="country">';
while ($row = $countryResult->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['id'] . '"' . ((isset($country) && $country == $row['id']) ? ' selected ' : '') . '>' . $row['country'] . '</option>';
}
echo "</select>";
echo '<div id="cityLayer"><select name="city" id="city">';
if (!empty($cityResult)) {
while ($cRow = $cityResult->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $cRow['id'] . '">' . $cRow['city'] . '</option>';
}
}
echo '</select></div>
<input type="submit" value="' . (!(isset($_POST['country']))?'Get cities':'Submit form') . '" />';
echo '</body>
</html>';

if ($_POST) {//are we responding to a form?
$country = $_POST['country'];
$sql_city = "SELECT * FROM city WHERE country_id =" . $country;
$cityResult = $db->query($sql_city);
if ((!$cityResult) || $cityResult->rowCount() == 0) {
die('could not pull city names for country ' . $country);
}
}
$sql_country = "SELECT * FROM country";
$countryResult = $db->query($sql_country);
if ((!$countryResult) || $countryResult->rowCount() == 0) {
die('no countries pulled from database');
}
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Country List</title>
</head>
<body>';
echo '<form id="frm" action="' . ((isset($_POST['city'])?'fullform.php':$_SERVER['PHP_SELF'])) . '" method="post">';
echo '<select name="country">';
while ($row = $countryResult->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['id'] . '"' . ((isset($country) && $country == $row['id']) ? ' selected ' : '') . '>' . $row['country'] . '</option>';
}
echo "</select>";
echo '<div id="cityLayer"><select name="city" id="city">';
if (!empty($cityResult)) {
while ($cRow = $cityResult->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $cRow['id'] . '">' . $cRow['city'] . '</option>';
}
}
echo '</select></div>
<input type="submit" value="' . (!(isset($_POST['country']))?'Get cities':'Submit form') . '" />';
echo '</body>
</html>';








Users browsing this forum: No registered users and 1 guest