Here I have an HTML file as follows. Inside the first tag I list the cities of a country drawing the data from a database. I want to draw the districts of the selected city to the second tag. However, I have a problem with transfering the selected city to my get_districts.php file. I tried various ways but any of them did not work. I can't find where the problem is.
<html>
<head>
<title>Ana Sayfa</title>
</head>
<body>
<header>
<div>
<div>
<div>
<a href="./index.html">Yemeksepeti</a>
</div>
<div>
<form>
<select id="city" name="city" onchange="getDistricts(this.value)">
<option value="City">İl</option>
<?php include 'action.php'; echo $output;?>
</select>
</form>
</div>
<div>
<select>
<option value="District">İlçe</option>
<embed id="district">
</select>
</div>
</div>
</div>
</header>
<script>
function getDistricts(str) {
if(str=="") {
return;
} else {
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if(this.readyState==4 && this.status==200) {
document.getElementById('district').innerHTML=this.responseText;
}
};
xmlhttp.open("POST","get_districts.php",true);
xmlhttp.send();
}
}
</script>
</body>
</html>
And this is my get_districts.php file
<?php
include "config.php";
if(isset($_POST['q'])):
$q = $_POST['q'];
echo $q;
$sql_districts="SELECT * FROM districts WHERE district_id = '".$q."'";
$districts = mysqli_query($conn,$sql_districts);
$city_districts='';
foreach($districts as $district)
{
$city_districts .='<option value="'.$district["district"].'">'.$district["district"].'</option>';
}
echo $city_districts;
else:
echo "hello";
endif;
?>