Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

icelander's avatar

how to add the value of a variable to my select input field

this is my select input field

<form action="payment.html" method="post" class="creditly-card-form agileinfo_form">
                              <section class="creditly-wrapper wrapper">
                                 <div class="information-wrapper">
                                    <div class="first-row form-group">
                                        <div class="input-group mb-3">
                                             <div class="input-group-prepend">
                                                <label class="input-group-text" for="inputGroupSelect01">Pharmacies</label>
                                             </div>
                                            
                                             <select class="custom-select" id="inputGroupSelect01">
                                                <option selected="">Choose...</option>
                                                <option value="">  </option>
                                                
                                             </select>
                                          </div>

this is my javascript code

function searchPharmacy(lat,lng){
               $.post('http://127.0.0.1:8000/api/checkout',{lat:lat,lng:lng},function(match){
                  // console.log(match);
                  $.each(match, function(i,val){
                   var pharmlatval=val.pharmacy_lat;
                   
                    var pharmlngval=val.pharmacy_lng;
                    var pharmname=val.pharmacy_state;
                    var picn='https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png';
                PLatLng = new google.maps.LatLng(pharmlatval, pharmlngval);

                createMarker(PLatLng,picn,pharmname);
                 document.getElementById('inputGroupSelect01').value=pharmname;
                 console.log(pharmname);

                  });
               });
            }

i want to output the value of the variable pharmname on the select input form field. how do i go about it.

0 likes
2 replies
Nakov's avatar

@icelander you can delete the empty option from the select, and use this instead:

let option = document.createElement("option");
option.text = pharmname;
option.value = pharmname;
document.getElementById('inputGroupSelect01').appendChild(option);
Tray2's avatar

Something like

let html = ''
values.forEach(function(value) {
    html += '<option value="' + value.value + '">' + value.text + '</option>'
})

document.querySelector('#id_of_select').innerHTML = html;

Please or to participate in this conversation.