dimanche 10 février 2019

How to get back ajax request sent to the controller on the view

I have a form

<form id="myForm">
    <div class="button dropdown">
      <select name="languageSelected" required="required" id="languageselector">
        <option value="">Select the language</option>
        @foreach($reviewForm_language as $lang)
         <option value=""></option>
        @endforeach
      </select>

    </div>
</form>

On selection; it makes a ajax request

 <script>    
   $(function() {
    // when select changes
    $('#languageselector').on('change', function() {
        // create data from form input(s)
        let formData = $('#myForm').serialize();

        // send data to your endpoint
        $.ajax({
            url: '/selected/languageId',
            method: 'post',
            data: formData,
            dataType: 'json', 
            success: function(response) {
                console.log(response); 
            }
        });
    });
  });
</script>

The route

Route::post('/selected/languageId','ProfileController@selectedLangId');

On the controller

public function selectedLangId(Request $request)
{
    \Log::info("Was here");
    return response()->json(['success'=> $request->languageSelected]);
}

This works well up to this point.

How do I get the $request->languageSelected passed to the controller back on the view? I would like to be assigned as a PHP variable to be used elsewhere.

Anyone?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire