Search code examples
ajaxlaravel-8crudedituncaught-reference-error

Laravel 8 crud using ajax: Uncaught ReferenceError: data_id is not defined


So I'm trying CRUD using ajax. Adding is fine but the Edit is not because of this error

Uncaught ReferenceError: data_id is not defined

Here is my trigger button:

@foreach($admin_quests as $quest)
//some codes
<button class="btn btn-success" type="button" id="editBtn" data_id="{{ $quest->id }}">Edit</button>
@endforeach

Here is the controller:

use Response;
//some codes

public function edit(AdminQuest $adminQuest)
    {
        return Response::json($adminQuest);
    }

Here is the script:

$('body').on('click','#editBtn', function(){
            $('#addUpdateModal').modal('show');
            $('#modalTitle').html("EDIT QUEST");

            console.log($(this).attr('data_id'));

//THIS IS WHERE THE PROBLEM STARTS
            //admin_quest/{admin_quest}/edit
            $.get("{{ route('admin_quest.index') }}" + '/' + data_id + '/edit', function(data){
                //some codes
        });

Solution

  • I forgot to put data_id = $(this).attr('data_id'); to define data_id.