title: Laravel - How to send data from a checkbox button to a controller using Ajax
body: <p>I am trying to have a toggle button (checkbox) changing some data in my MySQL database. I essentially want to to have a nice admin site in which I can unlock users to certain features on the site with a nice on/off switch. </p>
  
  <p>Everything seems to be set up correctly, but my controller doesn't react or get triggered at all.</p>
  
  <p>Right now, I have a working toggle button in my view triggering a javascript function. In that javascript function, I have already set up data that I want to transmit over to my controller. </p>
  
  <p>I have also set up Ajax in my Javascript. Apparently Ajax has this part in its code (see my code if you need to know what I mean) where it says "success" or "error". Before entering the {{csrf_token()}} token, I always ended up in the error section. However, now I always enter the "success" section when toggling my button.</p>
  
  <p>Now to my problem:
  Although Ajax confirms a success, literally nothing happens within my controller. I have no idea how to change this as there is no error code whatsoever, either.</p>
  
  <p>This is my button </p>
  
  <pre><code>&lt;input id = "company_{{$company-&gt;id}}" data-id="{{$company-&gt;id}}" onchange=myFunction({{$company-&gt;id}}) value = {{$company-&gt;permissionlevel}} class="toggle-class" type="checkbox" data-onstyle="success" data-offstyle="danger" data-toggle="toggle" data-on="Active" data-off="InActive" {{ $company-&gt;permissionlevel ? 'checked' : '' }}&gt;
  </code></pre>
  
  <p>This is my javascript (the relevant part)</p>
  
  <pre><code>var test = 12; // A random variable for this example
  
          $.ajax({
              method: 'POST', // Type of response and matches what we said in the route
              url: 'home/update', // This is the url we gave in the route
              data: {'test' : test, _token: '{{csrf_token()}}'}, // a JSON object to send back
              success: function(response){ // What to do if we succeed
                  console.log(response); 
                  //alert("Ajax success");
              },
              error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
                  console.log(JSON.stringify(jqXHR));
                  console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
                  //alert("Ajax error");
              }
          });
  </code></pre>
  
  <p>This is my controller function (just to see what happens)</p>
  
  <pre><code>public function update (Request $request)
      {
  
              //echo "This is a test";
              $data = $request-&gt;all(); // This will get all the request data.
              dd($data); // This will dump and die
              return $data;
  
  
  
          }   
  </code></pre>
  
  <p>Route</p>
  
  <pre><code>Route::post('/home/update', 'HomeController@update');
  </code></pre>
  
  <p>Expected result:
  Controller throws something at me. Currently all I want is to see if my controller reacts to the button press or if I can request the data Ajax is supposed to send over.</p>
  
  <p>Actual result:
  Nothing happens on my controller's side</p>