How to extract data from SELECT query in <select> as <option>

I just found out how to retrieve data from a database with this javascript (index.js) code on the server side;

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 result:

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 can I get this information and put it into my working .html file, each information should be inserted into the tag, as shown 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 file is called app.js for information only.

+4
source share
1 answer

assuming your data reaches the client through 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>"));
    }
}
+1
source

Source: https://habr.com/ru/post/1695802/


All Articles