通过Laravel 5.1中的AJAX将用户输入数据从视图传递到控制器

时间:2021-06-15 21:34:02

I want to send drop-down list data from view to controller via AJAX as a form variable using post method.

我想使用post方法通过AJAX将下拉列表数据从视图发送到控制器作为表单变量。

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

我可以使用get方法和使用路由参数将下拉列表数据从视图发送到控制器。

Here is my view code snippet:

这是我的视图代码段:

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 don't want to pass user input data using route parameters. Also I have multiple user input parameters which needs to send to controller hence above method is not feasible as well. Hence 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 ));

}

Dropdownlist code 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 check function:

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();      
   }
}

Now I am getting check function is not defined after changing ajax call as suggested. I am wondering what it the relation between on-select event of drop-down list and AJAX route?

现在我按照建议更改ajax调用后没有定义检查功能。我想知道下拉列表的选择事件和AJAX路由之间的关系是什么?

2 个解决方案

#1


0  

This is just an example for your understanding, I hope this will guide you to get your functionality.

这只是您理解的一个示例,我希望这将指导您获得您的功能。

if you don't want to show your data/parameter in URL, then you can also use Ajax Post,

如果您不想在URL中显示您的数据/参数,那么您也可以使用Ajax Post,

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

and in your routes, remove variable for query string, and make it like this.

并在您的路由中,删除查询字符串的变量,并使其像这样。

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

and in your controller

并在你的控制器

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 ));

}

#2


0  

Try this jquery answer:

试试这个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 

});

#1


0  

This is just an example for your understanding, I hope this will guide you to get your functionality.

这只是您理解的一个示例,我希望这将指导您获得您的功能。

if you don't want to show your data/parameter in URL, then you can also use Ajax Post,

如果您不想在URL中显示您的数据/参数,那么您也可以使用Ajax Post,

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

and in your routes, remove variable for query string, and make it like this.

并在您的路由中,删除查询字符串的变量,并使其像这样。

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

and in your controller

并在你的控制器

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 ));

}

#2


0  

Try this jquery answer:

试试这个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 

});