Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda