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

114
Vistas
want to populate datalist using json fetched from php using jquery

I want to populate datalist using json fetched from php using jquery.

HTML code : index.php

<body>
    <input type='text' id="gpanel" name="gpanel" list='listid' required>
    <datalist id='listid'>
      //i want my options here
   </datalist>
</body>

JQuery code :

<script>
$(document).ready(function() 
{
  $.getJSON("php/ajax.php", function(return_data){
     $.each(return_data.data, function(key,value){
     $("#listid").append(
     "<option value="+value.gpanel+">"+"</option>" //gpanel is the title i want in the values
    ); 
  });
});
</script>

php code : ajax.php

<?php
include("connection.php");
$result=mysqli_query($conn,"select * from active");//db name
while($rec = mysqli_fetch_assoc($result)) 
{
   $rows[] = $rec;
}
$json_row=json_encode($rows);
echo $json_row;
?>

I dont know where i did wrong! but the datalist is not populating as expected.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

In JQuery you are trying to loop throught return_data.data... Use $.each(return_data, function(key,value){ instead.

Or in PHP, add the data dimension :

$json_row=json_encode(array('data' => $rows));
about 4 years ago · Santiago Trujillo Denunciar

0

So simple!

HTML Code

<form>
    <h3>Search :</h3><br>
    <input type="text" id="search" list="datalist">
    <datalist id="datalist"></datalist>
</form>

jQuery Code

$("document").ready(()=>{
    $("#search").keyup(()=>{
        let list = $("#search").val();
        $("#datalist").empty();
        if(list!=""){
            $.getJSON("http://localhost/file.php?search="+list,(data)=>{
                $.each(data,(k,v)=>{     
                    $("#datalist").append("<option value="+v.gpanel+"></option>"); 
                });
            });
        }
    });
});

PHP Code file.php

$base = new PDO("mysql:host=URL;dbname=DATABASE_NAME","USER","PASSWORD");
$something = $_GET["search"];
$req = $base->prepare("SELECT * FROM your_table WHERE Row_name LIKE '%".$something."%'");
$req->execute();
$tab = $req->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($tab);
about 4 years ago · Santiago Trujillo Denunciar
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