Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda