I have a select with Countries and a Select with Cities and I want to display different values in the City Select depending on the selection of the Countries Select.
Something like: Value = countries.selection
SELECT city FROM cities WHERE Country = Value
$myrow[city]
See my Code Below (This bring values to the Selects from a Database and populated them.
There is probably a better way to code what I've code, I would also highly appreciate any suggestion to make it easier and more efficient.
Also, if it is better or easier to do it in Javascrip or jQuery I would accept suggestions in that direction as well.
Thank you so much
<html>
<body>
<select name="taskOption" id="drop_drop">
<?php
$db = pg_connect('host=localhost dbname=test user=myuser password=mypass');
$query = "SELECT country FROM countries";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
printf ("<option value=Select>Select a Country</option>");
while($myrow = pg_fetch_assoc($result)) {
printf ("<option value=$myrow[country]>$myrow[country]</option>");
}
?>
</select>
<select name="taskOption" id="drop_drop">
<?php
$db = pg_connect('host=localhost dbname=test user=myuser password=mypass');
$query = "SELECT city FROM cities";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
printf ("<option value=Select>Select a City</option>");
while($myrow = pg_fetch_assoc($result)) {
printf ("<option value=$myrow[city]>$myrow[city]</option>");
}
?>
</select>
</body>
</html>