简体   繁体   中英

laravel Get Value of input type text in ajax

I'm trying to get the value of an input type text to use it for the where clause in my Laravel controller query.

here is a part of the ajax code:

$(function() {
    $('#data-table').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax":{
             "url": "{{ route('item_data_table') }}",
             "dataType": "json",
             "type": "POST",
             "data":{ _token: "{{ csrf_token() }}"}
        },
        ...
        ...
        ...
    });
});

as for my trial, I tried changing:

"data":{ _token: "{{ csrf_token() }}"}

into:

"data":{value: $("input[name=categoryname]").val()}

Then in my controller, I have this part of query code, Tried it like this:

public function dataTable(Request $request)
    {
        $columns = [
            1 => 'id',
            2 => 'item_name',
            3 => 'item_category_name',
            4 => 'item_detail_category_name',
        ];

        $categoryname = Input::get('value');

        $totalData = MItem::count();
        $totalFiltered = $totalData;
        $limit = request()->length;
        $start = request()->start;
        $order = $columns[request()->order[0]['column']];
        $dir = request()->order[0]['dir'];
        $items = MItem::select('m_item.id',
                        'm_item.item_name',
                        'm_item_category.item_category_name',
                        'm_item_detail_category.item_detail_category_name'
                    )
                    ->join('m_item_category', 'm_item.item_category_id', 'm_item_category.id')
                    ->join('m_item_detail_category', 'm_item.item_category_detail_id', 'm_item_detail_category.id');
                    if($categoryname) {
                        $items->where('m_item_category.item_category_name', 'like', '%'.$userID.'%');
                    }
                    $items->offset($start)
                    ->limit($limit)
                    ->orderBy($order, $dir)
                    ->get();
        $data = [];

...
...
...
}

I used $cateogryname for my where clause but cannot succesfully do it.

How can be the code done?

You don't need to manually pass the token. Just set up it once and use it for all Ajax calls:

<meta name="csrf-token" content="{{ csrf_token() }}">

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

This will also allow you to not mix PHP with JS.

Try to pass data like this

 data: function (d) {
            d.name = $('input[name=name]').val();
            d.email = $('input[name=email]').val();
        }

u can find example here https://datatables.yajrabox.com/collection/custom-filter

Thanks for giving me ideas guys, somehow I did it like this, and got my search function working:

for the ajax code:

"ajax":{
         "url": "{{ route('item_data_table') }}",
         "dataType": "json",
         "type": "POST",
         "data":{
                _token: "{{ csrf_token() }}",
                'item_detail_category_name': $('#item_detail_category_name').val(),
                'item_category_name': $('#item_category_name').val(),
         }
}

and for the controller condition,

 $limit = request()->length;
        $start = request()->start;
        $order = $columns[request()->order[0]['column']];
        $dir = request()->order[0]['dir'];
        $items = MItem::select('m_item.id',
                        'm_item.item_name',
                        'm_item_category.item_category_name',
                        'm_item_detail_category.item_detail_category_name'
                    )
                    ->join('m_item_category', 'm_item.item_category_id', 'm_item_category.id')
                    ->join('m_item_detail_category', 'm_item.item_category_detail_id', 'm_item_detail_category.id');

        if(request()->item_category_name) {
            $items->where('m_item_category.item_category_name', 'like', '%'.request()->item_category_name.'%');
        }

        if(request()->item_detail_category_name) {
            $items->where('m_item_detail_category.item_detail_category_name', 'like', '%'.request()->item_detail_category_name.'%');
        }

        $totalData = $items->get()->count();
        $totalFiltered = $totalData;

        $data = [];

        $items->offset($start)->limit($limit)->orderBy($order, $dir);

        $items = $items->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM