Maryadi Poipo
Maryadi Poipo

Reputation: 1478

How to get selected checkbox ID from some checkboxes in Laravel 5.2

I'm creating some checkboxes in my laravel view as shown below :

<table class="table table-condensed table-bordered table-hover">
   <tr>
     <th>Report</th>
     <th>Type</th>
     <th>Date</th>
  </tr>
 @foreach($tableContent as $data)
  <tr>
  <td><input type="checkbox" name="report" value="reportValue" id="{{$data->id}}" >{{$data->title}} </input></td>
  <td><b>{{$data->report_type}}</b></td>
  <td>{{$data->created_at}}</td>
  </tr>
 @endforeach
 </table>
<a class="btn btn-default" href="/deletereport/"{{--how do I get checked box id here ? --}}">Delete</a>

So when user click the delete button, it will gonna call the route /deletereport/idcheckedbutton...

But I have no idea how do I get the selected button Id, Please help...:v

Thanks in advance.. :)

Upvotes: 1

Views: 2842

Answers (2)

FoFoCuddlyPoops
FoFoCuddlyPoops

Reputation: 147

I assume you are trying to delete every line that the user checked. If your checkboxes are inside a form element you don't need to pass the id in the url, the checked values will be available in your $request object.

Then you have to change your checkbox name to

<input type="checkbox" name="reports[{{$data->id}}]" value="reportValue" id="{{$data->id}}" >{{$data->title}}</input>

Then you can access this array in your controller.

$reports = $request->input('reports');

The array will look something like this:

[
    0 => 34 // the '0' is the $data->id and the '34' is the value assigned to the input
]

I have not been able to test this yet so let me know if this doesn't work.

Upvotes: 2

Martin Heraleck&#253;
Martin Heraleck&#253;

Reputation: 5779

You need to use JavaScript for this. In the onclick event of the button, just get the checked checkbox and redirect to the edited url:

$('a.btn').click(function()
{
    location.href = $(this).attr('href') + $('input[name="report"]:checked').attr('id');
});

Note that I'm using jQuery library.

Upvotes: 0

Related Questions