0

I'm using this script for changing the order of a table from ascending to descending:

@push('scripts')
        $("#dropdown-list").on('change', function(){
            var orderBy = document.getElementById("dropdown-list").value;
            $('#contentDiv').hide();
            $("#user-list").html('');
            if(orderBy == 'asc')
            {
                var orderBy = 1;
            } else if(orderBy == 'desc')
            {
                var orderBy = 2;
            }

            const url1 = "{{ route('admin.users.permissions' , ['user' => '__USER_ID__']) }}"
            const url2 = "{{ route('admin.users.wallet' , ['user' => '__USER_ID__']) }}"
            const url3 = "{{ route('admin.users.info' , ['user' => '__USER_ID__']) }}"
            const url4 = "{{ route('admin.users.edit' , ['user' => '__USER_ID__']) }}"
            $.ajax({
                url: "{{url('/admin/users/order-by')}}",
                type: "POST",
                data: {
                    order_by: orderBy,
                    _token: '{{csrf_token()}}'
                },
                dataType: 'json',
                success: function (result) {
                    $('#contentDiv').show();
                    $.each(result.orderby, function (key, value) {
                        const $row = $('<tr />');
                        $row.append('<td>' + value.id + '</td>');
                        $row.append('<td>' + value.name + '</td>');
                        $row.append('<td>' + value.email + '</td>');
                        $row.append('<td>' + value.is_active + '</td>');
                        const $btn_grp = $('<td class="d-flex"><div class="btn-group" />');
                        $btn_grp.append('<a href="' + url1.replace('__USER_ID__', '{!! $user->id !!}') + '" class="btn btn-sm btn-purple">Permissions</a>');
                        $btn_grp.append('<a href="' + url2.replace('__USER_ID__', '1') + '" class="btn btn-sm btn-info">Wallet</a>');
                        $btn_grp.append('<a href="' + url3.replace('__USER_ID__', '1') + '" class="btn btn-sm btn-warning">Information</a>');
                        $btn_grp.append('<a href="' + url4.replace('__USER_ID__', '1') + '" class="btn btn-sm btn-navy">Edit</a>');
                        const $btn_close = $('</div></td></tr>');
                        $btn_grp.append($btn_close);
                        $row.append($btn_grp);
                        $("#user-list").append($row);
                    });
                }
            });
@endpush

@component('admin.layouts.content' , ['title' => 'Users List'])
...
@endcomponent

So as you can see I tried passing a user variable id like this:

$btn_grp.append('id !!}') + '" class="btn btn-sm btn-purple">Permissions');

But somehow I get Undefined variable: user error:

enter image description here

So what's going wrong here? How can I properly pass the variable?

yetep93258
  • 33
  • 1
  • 13
  • You can not use PHP variables inside JS function as you wrote it in your code. – EHF Shahab Aug 10 '22 at 11:51
  • @EHFShahab So what is the correct way of passing this variable inside js? – yetep93258 Aug 10 '22 at 11:52
  • you need to pass user details along with the response so you can able to access that – Shibon Aug 10 '22 at 11:55
  • if you are trying to pass user_id pass it to data: { order_by: orderBy, _token: '{{csrf_token()}}', user_id:{{$user_id}} } – Shozab javeed Aug 10 '22 at 11:57
  • `$user` is not defined. Figure out why it's not defined and where it should be defined. You can actually use the PHP variables as shown above since it's in a blade template, not a js file, as long as the variable is already defined somewhere. – aynber Aug 10 '22 at 12:02

0 Answers0