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

AbdulBazith's avatar

How to pass multiple values through Ajax in laravel?

I am having form view.blade which shows the data of all sales_details (my table) in tabular format.

I am having three text boxes in my view.blade which I am using for search option.

This is my view.blade (just I copied the search box code only)


 <thead>

            <th > {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_name','name'=>'search_name')) }}</th>
            <th> {{ Form::text('search_area',null,array('class'=>'form-control','id'=>'search_area','name'=>'search_area')) }}</th>
            <th> {{ Form::text('search_booth',null,array('class'=>'form-control','id'=>'search_booth','name'=>'search_booth')) }}</th>

</thead>

This is my JavaScript


<javascript>

$('#search_name').on('keyup',function(){

$value1=$('#search_name').val();
$value2=$('#search_area').val();
$value3=$('#search_booth').val();
    
$.ajax({

type : 'get',

url : '{{URL::to('search_name')}}',

data:{'search_name':$value1
    'search_name':$value2
'search_name':$value3

},

success:function(data){

 $('tbody').html(data);

}

});

})

</script>

This is my controller


public function search_name( Request $request )

{
                    $output="";

                    $sales=Sales_details::where('customer_name','LIKE','%'.$request->search_name.'%')                  
                    ->orWhere('customer_area','LIKE','%'.$request->search_name.'%')
                    ->orWhere('customer_booth','LIKE','%'.$request->search_name.'%')
                   
                    ->paginate(10);
    
    if($sales)

    { 
        //Need to return the output to the ajax
    }
    else
    {
        // Need to perform some thing
    }
}

My doubt is I am writing the JavaScript in onkeyup event for the textboxes. I have entered some text in first text box and some text in second textbox an when I enter content in third textbox the ajax should fetch all the three content from three text boxes and it should pass to the function search() in the controller. [Will be better if any of the textbox is gets the onkeyup event the same ajax function must be called how?]

Then another doubt is after passing the three content those three must be used to where conditions in the function.

0 likes
13 replies
pardeepkumar's avatar

create a function in your controller like

public function abc(Request $request) {

if(!$request->ajax()){

// all the post data can be fetch using $request

}

}

In your route file create the url for your function.

Now you can use jQuery to do ajax call to your function

1 like
mvd's avatar

Hello AbdulBazith,

Add a onkeyup event on all the search fields?

In your ajax request add all the search field.

data:{
    'search_name':$value1
    'search_area':$value2
    'search_booth':$value3
},

In your controller something like this (not tested)

$sales = Sales_details::where(function ($query) use ($request->search_name, $request->search_area, $request->search_booth) {

    // Name.
    if (!empty($request->search_name)) {
      $query->orWhere('customer_name', 'LIKE', '%' . $request->search_name . '%');
    }

    // Area.
    if (!empty($request->search_area)) {
      $query->orWhere('customer_area', 'LIKE', '%' . $request->search_area . '%');
    }

    // Booth.
    if (!empty($request->search_booth)) {
      $query->orWhere('customer_booth', 'LIKE', '%' . $request->search_booth . '%');
    }

})->get();
1 like
AbdulBazith's avatar

@mvd i will try this, but here where is my function name and what the $query indicates?

$sales = Sales_details::where(function ($query) use ($request->search_name, $request->search_area, $request->search_booth) {

this line is showing full of error in my controller

1 like
mvd's avatar
mvd
Best Answer
Level 48

Name function in your controller? I only changed your $sales query in the search_name function

public function search_name( Request $request )

{
                    $output="";

                    $sales = Sales_details::where(function ($query) use ($request->search_name, $request->search_area, $request->search_booth) {

    // Name.
    if (!empty($request->search_name)) {
      $query->orWhere('customer_name', 'LIKE', '%' . $request->search_name . '%');
    }

    // Area.
    if (!empty($request->search_area)) {
      $query->orWhere('customer_area', 'LIKE', '%' . $request->search_area . '%');
    }

    // Booth.
    if (!empty($request->search_booth)) {
      $query->orWhere('customer_booth', 'LIKE', '%' . $request->search_booth . '%');
    }

})->paginate(10);
    
    if($sales)

    { 
        //Need to return the output to the ajax
    }
    else
    {
        // Need to perform some thing
    }
}

$query is a grouping functionality, see https://laravel.com/docs/5.6/queries Section 'Parameter Grouping'

1 like
AbdulBazith's avatar

@mvd sorry 3 days iam on leave iam unable to try ur code..

if i use this below code it shows error

$sales = Sales_details::where(function ($query) use ($request->search_name, $request->search_area, $request->search_booth) {

syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ')'

whats wrong in this??

another doubt my function has just one variable $request

the how it can get three values

see

public function search_name( Request $request )

1 like
Cronix's avatar

It should probably be just

$sales = Sales_details::where(function ($query) use ($request) {
// rest...
1 like
mvd's avatar

whats wrong in this??

Can you give us all the code of method 'search_name' ?

another doubt my function has just one variable $request

This is an object, variables $request->search_name, $request->search_area, $request->search_booth are in it.

1 like
AbdulBazith's avatar

@mvd and @Cronix both of you thank you sooo much

$sales = Sales_details::where(function ($query) use ($request) {

This works exactly thank you ...

There is another small doubt regarding this,,

in my view.blade the below code, Iam having three text boxes for search i need to call javascript function for all the three text boxes in onkeyup event. how it is possible



<thead>

            <th > {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_name','name'=>'search_name')) }}</th>
            <th> {{ Form::text('search_area',null,array('class'=>'form-control','id'=>'search_area','name'=>'search_area')) }}</th>
            <th> {{ Form::text('search_booth',null,array('class'=>'form-control','id'=>'search_booth','name'=>'search_booth')) }}</th>

</thead>


this is my javascript



<javascript>

$('#search_name').on('keyup',function(){

$value1=$('#search_name').val();
$value2=$('#search_area').val();
$value3=$('#search_booth').val();
    
$.ajax({

type : 'get',

url : '{{URL::to('search_name')}}',

data:{'search_name':$value1
    'search_name':$value2
'search_name':$value3

},

success:function(data){

 $('tbody').html(data);

}

});

})

</script>

When my first first text box is in onkeyup event then the search_name function in javascript must be called that is

$('#search_name').on('keyup',function(){

the above javascript..

here search_name is my class name of name textbox.

But what i expect is whatever my textbox is onkeyup event

then,


$value1=$('#search_name').val();
$value2=$('#search_area').val();
$value3=$('#search_booth').val();
    
$.ajax({

type : 'get',

url : '{{URL::to('search_name')}}',

data:{'search_name':$value1
    'search_name':$value2
'search_name':$value3

},

success:function(data){

 $('tbody').html(data);

}

});

})

this must be called how??

Cronix's avatar

You can target multiple elements...

$(function() {
    $('#search_name, #search_area, #search_booth').on('keyup', function() {

    $.ajax({
        type : 'get',
        url : '{{ URL::to('search_name') }}',
        data:{
            'search_name': $('#search_name').val(),
            'search_area': $('#search_area').val(),
            'search_booth': $('#search_booth').val()
        },
        success: function(data){
            console.log(data); // for debugging
            $('tbody').html(data);
        },
        error: function(response) {
            console.error(response); // for debugging
        }
    });
}
1 like
AbdulBazith's avatar

Wow @Cronix thanks a lot well worked thank you soo much

another small doubt in this itself pagination problm,

this is my controller,

public function index()
    {
        $sales = Sales_details::orderBy('created_at','desc')-   >paginate(5);

        return view('Sales_details.view')->withsales($sales);
    }


When i click view menu the index function is called and the below given view.blade file will run. there i displayed all the data in tabular format with pagination(5)

here is my view. blade below


@extends('main')

@section('title','| All data Sales')

@section('content')

<div class="row">
<div class="col-md-12">        

   <table  id="sales_details" class="table table-bordered">

   <thead>

            <th > {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_sid','name'=>'search_sid')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_date','name'=>'search_date')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_time','name'=>'search_time')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_id','name'=>'search_id')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_type','name'=>'search_type')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_name','name'=>'search_name')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_area','name'=>'search_area')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_booth','name'=>'search_booth')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_rl','name'=>'search_rl')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_nl','name'=>'search_nl')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_total','name'=>'search_total')) }}</th>
            <th> {{ Form::text('search_name',null,array('class'=>'form-control','id'=>'search_note','name'=>'search_note')) }}</th>
            <th> <a href="" class="btn btn-success btn-sm" style="width: 100%;">Search</a></th>

    </thead>


      <thead>

              <th >Sales Id</th>
              <th>Date</th>
              <th>Time</th>
              <th>Customer Id</th>
              <th>Customer Type</th>
              <th>Customer Name</th>
              <th>Customer Area</th>
              <th>Customer Booth</th>
              <th>Rate/Litre</th>
              <th>No of Litres</th>
              <th>Total</th>
              <th>Note</th>
              <th>Action</th>

      </thead>

      <tbody>
            @foreach($sales as $sal)
                <tr>
                    {{-- <th>{{ $ven->id }}</th> --}}
                    <td>{{ $sal->sales_id }}</td>
                    <td>{{ $sal->date }}</td>
                    <td>{{ $sal->time }}</td>
                    <td>{{ $sal->customer_id }}</td>
                    <td>{{ $sal->customer_type }}</td>
                    <td>{{ $sal->customer_name }}</td>
                    <td>{{ $sal->customer_area }}</td>
                    <td>{{ $sal->customer_booth }}</td>
                    <td>{{ $sal->rate_per_litre }}</td>
                    <td>{{ $sal->no_of_litre }}</td>
                    <td>{{ $sal->total }}</td>
                    <td>{{ $sal->note }}</td>

    <td>
        <a href="{{ route('Sales_details.show',$sal->id) }}" class="btn btn-success btn-sm" style="width: 100%;">View</a>

    </td>
    
  
             {!! Form::close() !!}  
       </tr>
            @endforeach
      </tbody>
      </table>
            <div class="text-center"> 
    {!! $sales->Links(); !!} 
            </div>
</div>
</div>
<script type="text/javascript">

    $('#search_sid,#search_date,#search_time,#search_id,#search_type,#search_name,#search_area,#search_booth,#search_rl,#search_nl,#search_total,#search_note').on('keyup',function(){

    $.ajax({

    type : 'get',

    url : '{{URL::to('search_name')}}',

    data:{ 'search_sid': $('#search_sid').val(),
           'search_date': $('#search_date').val(),
           'search_time': $('#search_time').val(),
           'search_id': $('#search_id').val(),
            'search_type': $('#search_type').val(),
            'search_name': $('#search_name').val(),
            'search_area': $('#search_area').val(),
            'search_booth': $('#search_booth').val(),
            'search_rl': $('#search_rl').val(),
            'search_nl': $('#search_nl').val(),
            'search_total': $('#search_total').val(),
            'search_note': $('#search_note').val()

    },
    success:function(data){

     $('tbody').html(data);

    }

    });

    })
    </script>
@endsection

When onkeyup event occurs in text box then the search_name function in my controller is called..

This is my search_name function in controller


public function search_name( Request $request )

    {
                    $output="";
                    $sales = Sales_details::where(function ($query) use ($request)
                       {

                         // Sales id.
                         if (!empty($request->search_sid)) {
                            $query->Where('sales_id', 'LIKE', '%' . $request->search_sid . '%');
                          }

                           // date.
                        if (!empty($request->search_date)) {
                            $query->Where('date', 'LIKE', '%' . $request->search_date . '%');
                          }

                           // time.
                        if (!empty($request->search_time)) {
                            $query->Where('time', 'LIKE', '%' . $request->search_time . '%');
                          }

                           // customer id.
                        if (!empty($request->search_id)) {
                            $query->Where('customer_id', 'LIKE', '%' . $request->search_id . '%');
                          }

                           // type.
                        if (!empty($request->search_type)) {
                            $query->Where('customer_type', 'LIKE', '%' . $request->search_type . '%');
                          }

                        // Name.
                        if (!empty($request->search_name)) {
                          $query->Where('customer_name', 'LIKE', '%' . $request->search_name . '%');
                        }

                        // Area.
                        if (!empty($request->search_area)) {
                          $query->Where('customer_area', 'LIKE', '%' . $request->search_area . '%');
                        }

                       // Booth.
                        if (!empty($request->search_booth)) {
                          $query->orWhere('customer_booth', 'LIKE', '%' . $request->search_booth . '%');
                        }

                        // rate per litre.
                        if (!empty($request->search_rl)) {
                            $query->Where('rate_per_litre', 'LIKE', '%' . $request->search_rl . '%');
                          }

                         // No of litres.
                         if (!empty($request->search_nl)) {
                            $query->Where('no_of_litre', 'LIKE', '%' . $request->search_nl . '%');
                          }

                           // total.
                        if (!empty($request->search_total)) {
                            $query->Where('total', 'LIKE', '%' . $request->search_total . '%');
                          }

                           // note.
                        if (!empty($request->search_note)) {
                            $query->Where('note', 'LIKE', '%' . $request->search_note . '%');
                          }

                    })->get();

                     if($sales)

                    {

                    foreach ($sales as $key => $sale)
                    {

                    $output.='<tr>'.

                    '<td>'.$sale->sales_id.'</td>'.

                    '<td>'.$sale->date.'</td>'.

                    '<td>'.$sale->time.'</td>'.

                    '<td>'.$sale->customer_id.'</td>'.

                    '<td>'.$sale->customer_type.'</td>'.

                    '<td>'.$sale->customer_name.'</td>'.

                    '<td>'.$sale->customer_area.'</td>'.

                    '<td>'.$sale->customer_booth.'</td>'.

                    '<td>'.$sale->rate_per_litre.'</td>'.

                    '<td>'.$sale->no_of_litre.'</td>'.

                    '<td>'.$sale->total.'</td>'.

                    '<td>'.$sale->note.'</td>'.

                    '<td>'

                    ."<a href='' class='btn btn-success btn-sm' style='width: 100%;'>View</a>".

                    '</td>'.

                    '</tr>';

                    }               

                    return Response($output);
                    }
    }

now based on my where query the results are filtered so here i need to give pagination what should i do??

if i give pagination(2) in this below line, it doesnt work why?


$sales = Sales_details::where(function ($query) use ($request) {
//those if condition with where clause
 })->Paginate(2);

Someone help me in this pagination problem pleasee

Snapey's avatar

Don't call out to specific people

Dont write "it doesnt work"

Please or to participate in this conversation.