Passing user input from a view to a controller through AJAX in Laravel 5.1

I want to send dropdown data from a view to a controller via AJAX as a form variable using the post method.

I can send the drop-down list data from the view to the controller using the get method and using the route parameters.

Here is the code snippet of my view:

function drawChart(frmyear, toyear) 
{

    console.log(frmyear);
    console.log(toyear);


        var jsonData = $.ajax({
        url: "get_salesthree/"+ frmyear + "/"+ toyear +"/",
        dataType: 'json',
        async: false
                    }).responseText;

        console.log(jsonData);

Route code snippet:

    Route::get('get_salesthree/{frmyear}/{toyear}', array('uses'=>'Analytics\DashboardController@get_salesthree'));

For security reasons, I do not want to pass user input using route parameters. In addition, I have several user input parameters that need to be sent to the controller, so the above method is also not possible. Therefore, any other alternative solution available in this case?

Controller code snippet:

public function get_salesthree($frmyear, $toyear)
{   

     return \Response::json(Salethree::get_formatted_salesthree($frmyear, $toyear ));

}

Snippet snippet:

<label>From Date</label>
                    <select id="ddlfrmyear" name="frmyear" onchange="check(this);">
                    <option value="-1">Select Date </option>
                        @foreach ($date_lists as $date_list)
                    <option value="{{ $date_list}}">{{ $date_list}}</option>
                        @endforeach
                    </select>

JavaScript:

function check(sel) 
{
   document.getElementById('ddltoyear').disabled = !sel.selectedIndex;
   var frmyear =  document.getElementById('ddlfrmyear').value;

   var toyear =  document.getElementById('ddltoyear').value;

   console.log(frmyear);    
   console.log(toyear);
    if (toyear != '-1')
    {
        drawChart(frmyear, toyear);
        //drawChart();      
   }
}

, ajax-, . , on-select AJAX?

+4
2

, , .

/ URL-, Ajax Post,

$.ajax({
        type: 'POST' 
        url: "get_salesthree/",
        data: yourForm.serialize()
        dataType: 'json'

.

Route::post('get_salesthree/', array('uses'=>'Analytics\DashboardController@get_salesthree'));

public function get_salesthree(Request $request){   
     $frmyear = $request->input('frmyear');
     $toyear  = $request->input('toyear');

     //do your functionality as per your need


     return \Response::json(Salethree::get_formatted_salesthree($frmyear, $toyear ));

}
0

jquery:

JS:

$('form').submit(function(e){
e.preventDefault();
 var frmyear =  $('#ddlfrmyear').val();
  var toyear =  $('#ddltoyear').val();

$.ajax({
        type: 'POST' 
        url: "get_salesthree/",
        data: {frmyear:frmyear ,toyear:toyear},
        dataType: 'json',
        success:function(data){
          console.log(data);
        }});
  });   

Laravel:

Route::post('get_salesthree/', function(Request $request){

 $frmyear = $request->input('frmyear');
  $toyear  = $request->input('toyear');

     return \Response::json(Salethree::get_formatted_salesthree($frmyear, $toyear 

});
0

Source: https://habr.com/ru/post/1626972/


All Articles