Beginner Beginner - 7 days ago 6
Ajax Question

Loading data to html is not working

I have two pages (html & js),I have data returned from ajax (from database),i made spatial query on it and i want to load some of its data in an input (listbox) in html page but it is not working (no data is loaded in the listbox).
This is the part of html:

<div>
<select name="FarmersID" id="FarmersID" multiple style="width:172px">
</select>
</div>


and this is the ajax part

$.ajax({
type:"POST",
url:"CustomerID_geojson.php",
data:{'OrdersID': Order_ID} ,
dataType: 'json',
success: function (response) {
var unit = 'kilometers';
var buffered = turf.buffer(response, distance, unit);
bufresult = buffered.geometry.coordinates;
$.ajax({
type: "POST",
url: 'allfarmers_geojson.php',
dataType: 'json',
success: function (data)
{
var searchWithin = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": bufresult
}
}
]
};

var ptsWithin = turf.within(data,searchWithin);
//to check if there are any farmers within the distance or not
if (ptsWithin.features.length > 0)
{
geojsonLayer = L.geoJson(ptsWithin,
{
onEachFeature: function (feature, layer)
{
FarmerID=feature.properties.Farm_id;
document.getElementById('FarmersID').value=FarmerID; ///This one is not working
layer.bindPopup('<label>Farmer Name:</label>' + feature.properties.nick_name_ + '<br><label>Farmer ID:</label>' + feature.properties.Farm_id);
}
}).addTo(mymap);
mymap.fitBounds(geojsonLayer.getBounds());

}
else
{
alert("No Farmers Found!");
}
}
});

}
});

Answer
$('#FarmersID').append($('<option>', {
    value: 1,
    text: 'My option'
}));

reffer this anwer

I hope this is what u want to do add options to your select item via ajax response.

Comments