Home > Software design >  Sending selected ids with one button with laravel checkbox
Sending selected ids with one button with laravel checkbox

Time:08-26

There are id values ​​as in the photo below and I want to send them into a function, but in the examples I have examined, they are not sent with a single button, so since it is in the foreach, the button is constantly produced. How can I send the selected id values ​​with a single button?

ID's

<div >
<input id="tea-submit" type="submit1"  name="submit1" value="Send">
</div>


    @foreach ($pks as $pk)
     <tr>
     <td >
     <form action="{{route('admin.pks.calculate')}}" method="POST">
      @csrf
     <input type="checkbox" name="pk_id[]" value="{{$pk->id}}">
      </form>
      {{ $pk->id }}
     </td>
     </tr>
    @endforeach

public function calculate(Request $request)
    {
        $pk_ids = $request->input('pk_id');
        dd($pk_ids);
    }

CodePudding user response:

You need only one form element and do the foreach to the inputs, like this:

<form action="{{route('admin.pks.calculate')}}" method="POST">
  @csrf
  @foreach($pks as $pk)
     <tr><td >
        <input type="checkbox" name="pk_id[]" value="{{ $pk->id }}"> {{ $pk->id }}
     </td></tr>
   @endforeach
   <button type="submit">Send</button>
</form>
  • Related