简体   繁体   中英

Laravel - pass 2 variables from view to controller with ajax

I have this function for my delete button

<a href="#" onclick='deleteFile("{{ ($event->id) }}", "{{ ($file->name) }}")' class="btn btn-danger btn-xs delete">L&ouml;schen</a>

and the route in routes/web.php

Route::post('/delete-file', 'MyController@deleteEventFile');

that hits this function

public function deleteEventFile($eventid, $filename){
   dd($eventid);
}

and this is my ajax function:

function deleteFile(eventid, filename){
    //alert(filename);
    //alert(eventid);
    $.ajax({
      url: '/delete-file/',
      type: "post",
      data:{ _token: "{{csrf_token()}}", eventid: eventid, filename: filename },
      dataType: 'json',
    });
}

And i always get this error:

Missing argument 1 for App\Http\Controllers\MyController::deleteEventFile()

my variables can't get through... How to pass the eventid and filename to controller

According to your code, you are expecting route to give 2 params - eventid & filename into the controller method.

Instead it you should code your method like this:

public function deleteEventFile() {
   $event_id = request()->get('eventid');
   $file_name = request()->get('filename');
}

Fetch the POST data from the laravel's request() method instead.

Hope this helps!

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