Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

378
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

why you gave class to your select there can be multiple dropdown?

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!