Gargantua
Gargantua

Reputation: 430

how to fetch data from SELECT query to a <select> as a <option>

i've just find out how to fetch data from database with this code in my server side javascript (index.js);

con.connect(function(err) {
con.query("SELECT Cliente FROM Tab_Clienti;", function (err, result, fields) {
      if (err) throw err;
      console.log(result);
    });
});

and this is part of the output:

RowDataPacket { Cliente: 'GALLERIA DELL ACCADEMIA DI FIRENZE AUDIO - VIDEO'},
RowDataPacket { Cliente: 'ZETEMA AUDIO - VIDEO' },
RowDataPacket { Cliente: 'TERRE DES HOMMES AUDIO - VIDEO' },
RowDataPacket { Cliente: 'ISMETT AUDIO - VIDEO' },
RowDataPacket { Cliente: 'PRAGMATIKA AUDIO - VIDEO' },
RowDataPacket { Cliente: 'CANTINA DI SOAVE AUDIO - VIDEO' },

how do i get those info and put into my workspace.html, each data needs to be insert into a tag, like below:

<select multiple class="form-control" id="clienti">
       <option>1</option>
       <option>2</option>
       <option>3</option>
       <option>4</option>
       <option>5</option>
</select>

my client side file is called app.js just for info.

Upvotes: 4

Views: 1520

Answers (1)

Aniruddha Gohad
Aniruddha Gohad

Reputation: 253

assuming that your data reaches the client via ajax and is an array : in your ajax success function :

success : function(response){
    for(var i = 0; i < response.length; i++){
        $("#clienti").append($("<option>"+response[i]["Cliente"]+"</option>"));
    }
}

Upvotes: 1

Related Questions