Home > front end >  Update automaticaly switch button on checked
Update automaticaly switch button on checked

Time:11-24

I have issues in my project. In deed, I want to update automatically a switch button when I check it. The case that I have is that when I click on the switch button, the page is refresh but the value is not changed.

my code :

<div class="col-4 text-left">
     <form  id="form" class="was-validated" method="POST" action="{{ route("admin.patients.update", [$patient->id]) }}" enctype="multipart/form-data">
     @method('PUT')
     @csrf
         <div style="float: right">
              <label hljs-keyword">switch">
                  <input type="checkbox" onchange="$('#form').submit();" name="cas_suivi" id="cas_suivi" type="checkbox" {{  ($patient->cas_suivi == "cas_suivi" ? ' checked' : '') }} >
                      <div hljs-string">"></div>
              </label>
          </div>
       </form>
     </div>

the controller:

public function update(UpdatePatientRequest $request, Patient $patient) { $patient->update($request->all()); return view( 'admin.patients.show', (compact('patient'))->with('success', 'Le patient a bien été modifié'); }

CodePudding user response:

The result is:

    array:3 [▼
    "_method" => "PUT"
    "_token" => "Vo93U0F1uO1Ji7r03uVuENgq8JylZsEV5u6zQfRV"
    "cas_suivi" => "on"
    ]

CodePudding user response:

After updating the table you need to redirect to the page again.

//change the route name if necessary

public function update(UpdatePatientRequest $request, Patient $patient) 
    { 
        $patient->update($request->all()); 
        return redirect()
        ->route('admin.patients.index')
        ->with('success', 'Le patient a bien été modifié'); 
    }
  • Related