'Insert into multiple input fields using select2 and ajax in laravel
I am trying to use a single select request to retrieve data from database based on the search term "name" and populate my input fields using select2 so that i don't need to fill in the forms again after the selection but seems not getting anywhere as i have search across the web but not able to find anything that solve the problem
here is my select request
<script type="text/javascript">
$('.serviceId').select2({
placeholder: 'Select Service',
allowClear: true,
ajax: {
url: '/autocomplete/fetch',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.name,
id: item.id,
description:item.description,
rate:item.rate
}
})
};
},
cache: true
}
});
and here is the form;
<select class="form-select form-select-solid fw-bold serviceId" name="serv_name"></select>
</td>
<td>
{{ Form::text('description[]', null, ['class' => 'form-control form-control-solid']) }}
</td>
<td class="table__qty">
{{ Form::number('quantity[]', null, ['class' => 'form-control qty form-control-solid','required', 'type' => 'number', "min" => 1]) }}
</td>
<td>
{{ Form::text('rate[]', null, ['class' => 'form-control price-input rate form-control-solid','required']) }}
</td>
and the controller file
/**
*show the application fetch
*
*@return \Illuminate\Htttp\Response
*/
public function fetch(Request $request) {
$data = [];
if ($request->has('q')) {
$search = $request->q;
$data = DB::table('services')
->orderby('name','asc')
->select('id','name', 'description', 'rate')
->where('name', 'like', '%' .$search . '%')
->limit(5)->get();
}
return response()->json($data);
}
my route: web.php
Route::get('/autocomplete/fetch', 'InvoiceController@fetch')->name('autocomplete.fetch');
i only need the qty field to be hard code by the user.
sorry am not so good with codes and any valuable help would be much appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|