2

Laravel 5.5

I have a search view where users can type in the text input fields to search a table. The request goes to GET variables onto the results blade.

Inside the results blade, there is a Laravel ajax table. https://datatables.yajrabox.com/

I can't figure out how to get pass the GET variables in the URL to the search Laravel ajax request.

Ultimately, I want to be able to pass $_GET lead_name, lead_phone, etc to this ajax request. How can I accomplish that?

Here is my datatables script at the bottom of the page:

<script>
$(function() {
    $('.datatable').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '{{ route('SearchResults') }}',
            method: 'GET'
        },
        columns: [
            { data: 'lead_name', name: 'leads.lead_name' },
            { data: 'lead_merchant_id', name: 'leads.lead_merchant_id'},
            { data: 'lead_address_city', name: 'leads.lead_address_city' },
            { data: 'lead_address_state', name: 'leads.lead_address_state' },
            { data: 'accountstatus_description', name: 'accountstatus.accountstatus_description' },
            { data: 'firstlast_name', name: 'name' }
        ],
        rowReorder: {
            selector: 'td:nth-child(0)' 
           },
        responsive: true
    });
});

If I run a dd on my searchresults route:

public function SearchResults(request $request) {
    dd($request);
}

it doesn't return any of the previous GET data.

4
  • What is the response? Are you getting past the csrf middleware? Commented Feb 5, 2018 at 22:51
  • @TarekAdam It's showing some weird data such as the column names, order, start, length, etc which are not any of my get variables. The library datatables.yajrabox.com that I'm using takes care of any csrf middleware. Commented Feb 5, 2018 at 23:05
  • Is all that stuff in the post response or get response? Commented Feb 5, 2018 at 23:09
  • @TarekAdam It's in both. The data I'm needing to pass to the ajax is from the GET params in the url. Commented Feb 5, 2018 at 23:10

2 Answers 2

5

No need to send by GET method , You can easily use the POST method in the following way :

You should have a form on your page with the POST method and unique ID

<form method="POST" id="search-form" role="form">
      {{csrf_field()}}
      <input type="text" name="lead_phone">
</form>

On your page, write this jquery code that runs this code when you submit the form

$(document).ready(function(){
    $('#search-form').on('submit', function(e) {
        table.draw();
        e.preventDefault();
    });
});

Then use the following code for your DataTable section

$(function() {
  var table = $('.datatable').DataTable({
    processing: true,
    serverSide: true,
    ajax: {
        url: '{{ route('SearchResults') }}',
        type: "post",
        data: function (d) {
              d.lead_phone = $('input[name=visited]').val();
              d._token = _token;
        },
    },
    columns: [
        { data: 'lead_name', name: 'leads.lead_name' },
        { data: 'lead_merchant_id', name: 'leads.lead_merchant_id'},
        { data: 'lead_address_city', name: 'leads.lead_address_city' },
        { data: 'lead_address_state', name: 'leads.lead_address_state' },
        { data: 'accountstatus_description', name: 'accountstatus.accountstatus_description' },
        { data: 'firstlast_name', name: 'name' }
    ],
    rowReorder: {
        selector: 'td:nth-child(0)' 
       },
    responsive: true
  });
});

If the data is sent by Query String, there's no need for a jquery code and form, just write down the following code in your DataTable section

$(function() {
  var table = $('.datatable').DataTable({
    processing: true,
    serverSide: true,
    ajax: {
        url: '{{ route('SearchResults') }}',
        type: "post",
        data: function (d) {
              d.lead_phone = '{{$_GET['lead_phone']}}';
              d._token = '{{csrf_token()}}';
        },
    },
    columns: [
        { data: 'lead_name', name: 'leads.lead_name' },
        { data: 'lead_merchant_id', name: 'leads.lead_merchant_id'},
        { data: 'lead_address_city', name: 'leads.lead_address_city' },
        { data: 'lead_address_state', name: 'leads.lead_address_state' },
        { data: 'accountstatus_description', name: 'accountstatus.accountstatus_description' },
        { data: 'firstlast_name', name: 'name' }
    ],
    rowReorder: {
        selector: 'td:nth-child(0)' 
       },
    responsive: true
  });
});

And then get the data you send to controller

public function DataTable(Request $request)
{
   $lead_phone = $request->get('lead_phone');
   .
   .
   .
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting error like below in this. Uncaught ReferenceError: table is not defined. I am using type post in ajax request and I have to receive data in the same page from where the form is posted.
0

Please specify the method of sending data.

<script>
$(function() {
    $('.datatable').DataTable({
        processing: true,
        serverSide: true,
        method: post,
        ajax: '{{ route('SearchResults') }}',
        columns: [
            { data: 'lead_name', name: 'leads.lead_name' },
            { data: 'lead_merchant_id', name: 'leads.lead_merchant_id'},
            { data: 'lead_address_city', name: 'leads.lead_address_city' },
            { data: 'lead_address_state', name: 'leads.lead_address_state' },
            { data: 'accountstatus_description', name: 'accountstatus.accountstatus_description' },
            { data: 'firstlast_name', name: 'name' }
        ],
        rowReorder: {
            selector: 'td:nth-child(0)' 
           },
        responsive: true
    });
});

1 Comment

Tried this.. the default method is POST. You don't have to declare it unless needed. I'm needing a GET request. If I change it to get and run a `dd(request $request) on the ajax request it doesn't return the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.