How to add options to a drop-down list using jQuery?

Asked

Viewed 544 times

0

I have a role in jQuery who receives a array from PHP, through the $.post. The array comes like this:

array(
    '0' => array(
        'ID' => '1',
        'NOME' => 'João'
    ),
    '1' => array(
        'ID' => '2',
        'NOME' => 'Maria'
    ),
    '2' => array(
        'ID' => '3',
        'NOME' => 'Marcos'
    ),
) 

How do I stop through jQuery, add option in a select with the data from that array? Example:

<select>
 <option value="ID">"NOME"</option>
</select>

This is the $.post that I’m receiving the array from php

$.post('/consulta/usuario', {id:id}, function(dados){
        alert(dados);
}, 'json');

I’ll get the id according to the option that the user chooses, and next to php I am returning the array:

echo json_encode($usuario);
  • What gives alert(typeof dados);? gives string?

  • @Sergio does not give anything, the box of Alert does not appear

  • See something wrong with your browser tools? see the request made and the return? (Veloper tools > network)

  • @Sergio after much snooping, I realized that he is giving error because some of the fields of the array are coming with BD accent

  • And fixing that already works? what kind of mistake gave?

  • @Sergio worked, was giving error on account of utf8, now I managed to do

Show 1 more comment

2 answers

1

I assume you receive a PHP JSON, which you have in PHP:

echo json_encode($array);

So in Javascript you can iterate this object like this:

var select = document.querySelector('select');
pessoas.forEach(function(obj) {
    var opt = document.createElement('option');
    opt.value = obj.ID;
    opt.innerHTML = obj.NOME;
    select.appendChild(opt);
});

jsFiddle: https://jsfiddle.net/g26bjL3r/

  • I don’t think I understand very well pure Javascript, in which part are you getting the $array in it?

  • @Fleuquerlima because it depends a little on how you are passing data from PHP to Javascript. The PHP array would be pessoas. How are you getting this in Javascript?

  • I am using the $.post() of jquery, when I return the array with print_r for example it appears the data, when I tried to put the json as they said it comes back blank.

  • @Fleuquerlima can put this code $.post on the question? What gives for example alert(typeof dados); within that $.post?

  • Yes I am seeing the return with use of Alert, I will edit the question

1


has two forms, using a tie each or for.

I’ll demonstrate using the for

for(var i = 0; i < data.length; i++) {
    var id    =   data[i]['ID'];
    var name  =   data[i]['NOME'];
    var $option  =  $( '<option />' ).val(id).text(name);
    $( 'select' ).append( $option );
}

I will leave the form of each() by your own, it is a more pleasant implementation, you can search this on the link: Jquery $each()

I hope it helps you

  • the date variable is the variable that receives the Response of the ajax method that returns the json of the array.

  • I’m trying here to unsuccessfully, it just adds several empty options...

  • I am using the $.post() of jquery, when I return the array with print_r for example it appears the data, when I try to use the json it appears blank..

  • Managed to solve friend ?

  • I did! but using the for you explained to me, I couldn’t use each()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.