Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
How to transfer a data selected in <select> tag to php and then return the obtained value inside another <select> element

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;
?>

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!