3

I have List of the question and All question is print in a foreach loop. I want to store some question id in the database using checkbox.

@foreach ($allquestion as $indexKey => $all )
          <tr>
            <th>{{$indexKey+1}}</th>
            <th>{{substr($all->question,0,20)}} {{ strlen($all->question)>20 
             ? "..." : ""}}</th>
            <td>{{$all->option1}}</td>
            <td>{{$all->option2}}</td>
            <td>{{$all->option3}}</td>
            <td>{{$all->option4}}</td>
            <td>{{$all->answer}}</td>




            <td>
               <div class="form-check">
                 <input class="form-check-input big-checkbox" 
            name="present[]" type="checkbox" value="{{$all->id}}"   
          id="defaultCheck1">
              </div>
           </td>

          </tr>  @endforeach

I have no idea where I put submit button and how to get value from a checkbox into Controller to save data.

updated

{!! Form::open(array('route' => 'addq', 'data-parsley-validate' => '', 
'files' => true,'method'=>'post')) !!}
 @foreach ($allquestion as $indexKey => $all )
          <tr>
            <th>{{$indexKey+1}}</th>
            <th>{{substr($all->question,0,20)}} {{ strlen($all->question)>20 ? "..." : ""}}</th>
            <td>{{$all->option1}}</td>
            <td>{{$all->option2}}</td>
            <td>{{$all->option3}}</td>
            <td>{{$all->option4}}</td>
            <td>{{$all->answer}}</td>




            <td>
               <div class="form-check">
           <input type="checkbox" class="form-check-input big-checkbox"  
 name="checked[]" value="{{ $all->id }}">
              </div>
           </td>
          </tr>

      @endforeach
    {{Form::submit('Create Test',['class'=>'btn btn-primary'])}}
    {!! Form::close() !!}

When I Put Button like this no action perform.

3 Answers 3

4

The same functionality I implemented for one my project and it's working fine,please check the below code. I think it will be useful for you.

HTML:

<input type="checkbox" name="checked[]" value="{{ $employee->id }}">

PHP:

public function store(Request $request)
 {
   foreach ($request->employee_id as $key => $val) 
    {
     $payrolls = new Payroll;
      if (in_array($val, $request->checked))
       {
         $payrolls->basic = $request->basic[$key];
         $payrolls->employee_id = $val;
         $payrolls->save();
       }
    }
return redirect('/');
 }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, Deepti, can you help me to add submit button in this code
@brij Yes, you can add HTML submit button. What's the issue on that?
when I put Button after loop noting happen and when I put a button in the loop. it shows every row.
when i put button inside the loop it's show error. "Invalid argument supplied for foreach()"
If you are getting error "Invalid argument supplied for foreach()" with the following code foreach ($request->employee_id as $key => $val) Try doing it like foreach ($request->checked as $key => $val)
0

You Should Try this

Blade File

<form action="Your route">
@foreach ($allquestion as $indexKey => $all )
         <tr>
           <th>{{$indexKey+1}}</th>
           <th>{{substr($all->question,0,20)}} {{ strlen($all->question)>20
            ? "..." : ""}}</th>
           <td>{{$all->option1}}</td>
           <td>{{$all->option2}}</td>
           <td>{{$all->option3}}</td>
           <td>{{$all->option4}}</td>
           <td>{{$all->answer}}</td>

           <td>
              <div class="form-check">
                <input class="form-check-input big-checkbox" name="present[]" type="checkbox" value="{{$all->id}}" id="defaultCheck1">
             </div>
          </td>

         </tr>  
@endforeach
<button type="submit" class="btn btn-primary save">Submit</button>
</form>

In controller:

$present = $request->get('present');
dd($present);

Comments

0

HTML:

<input type="checkbox" name="room[]" value="{{ $service->id }}">

PHP:

foreach($request->room as $val){
   $data = new ServiceForRoom();
   $data->service_id = $val;
   $data->room_id = $room->id;

   $data->save()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.