Andrew Vanusi Andrew Vanusi - 23 days ago 7
jQuery Question

Append an array values into select option Jquery

Im currently using Laravel

here's my jquery code so far :

$('button[type="search"]').click(function(e) {
$.ajax({
url: "{{ route('car.plate_no') }}",
type: "POST",
data: {
'_token' : '{{csrf_token() }}',
'driver_id' : $('select[name="driver_id"]').val(),
'fine_date' : $('input[name="fine_date"]').val(),
},
success: function(data) {
if(data.status == true) {
var result = '';

$.each(data.getCarbyDriver, function(i, data) {
result += '<div>'+ data.plate_no +'</div>';
});
$('#search-result').html(result);
}
},
error: function(data) {

}
});
});


and here's my form code so far :

<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group filter-btn">
<button class='btn btn-info' type='search'>Search</button>
</div>
</div>

<select id="search-result"></select>


everything works fine, data is being returned.

how do i append plate_no data into select box and store it to database .

any idea ?

Answer

Javascript: inside foreach:

result += '<option value='+data.plate_no+'>'+data.plate_no+'</option>';
$('#search-result').html(result);

html:

<select name="" id="search-result">
</select>