Home > OS >  How to display multiple data from a checkbox when checked?
How to display multiple data from a checkbox when checked?

Time:08-29

I have multiple checkboxes, I want to display an array of data when multiple checkboxes are clicked, but only one value is displayed, how to display an array ?What is the problem?

  <div  >
                <h5>Your languages</h5>
                <div >
                    @foreach($langs as $key => $lang)
                 <input type="checkbox" name="foo[]"  value="{{$key}}"> 
                 <label>{{ $lang }}</label>, 
                  @endforeach

                </div>
            </div>

To controller

public function Method(Request $request)
 {
    foreach((array)$request->input('foo') as $value){
    $file = 'la.txt';
    file_put_contents($file,$value );
    }

     return redirect()->route('profile');
     
 }

I want to display the value of all these three checkboxes, but only the data of one of them is displayed

enter image description here

CodePudding user response:

Hmm - let me guess the issue, I think that you should return the values while you return back to the profile view. I think also the problem is not in the first load of the profile view, because you have to pass the langs array, but the issue happens when you click the checkboxes to store the data in the controller's method, so your code must be like so

public function storeData(Request $request)
 {
    foreach($request->input('foo') as $value){
        $file = 'la.txt';
        file_put_contents($file, $value);
    }

    $yourLangsArray = ["English", "Arabic"];

    return redirect()->route('profile')->with('langs', $yourLangsArray);  
 }
  • Related