dousong9729
2018-04-26 09:33
浏览 178

如何通过ajax将数据数组发送到后端(php)

I need some help here guys, my issue is that the first console.log returns what has been built by the foreach, but the backend doesn't receive that same array. This foreach basically iterates over a table, looks for a certain id, makes it an index of an array, and then it fetches all the input values from the table and pushes it to that given index.

        var dateTime = [];
        $("#employeeData > tr").each(function(dateIndex, date) {
            dateTime[date.id] = [];
            $("#" + date.id + " :input").each(function(inputIndex, inputTime) {
                dateTime[date.id].push($(inputTime).val())
            });
        });
        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        console.log(dateTime);
        $.post('employee',
            {
                dates: dateTime,
                _token: CSRF_TOKEN
            }, function(response) {
                console.log(response);
            }
        );
        });
    Route::post('/employee', 'EmployeesController@updateRecords');

    public function updateRecords(Request $request)
    {
        return $request->dates;
    }
    // this is a console.log of the dateTime, each date has 4 inputs 
    //associated with it
    [2017-11-02: Array(4), 2017-11-03: Array(4), 2017-11-06: Array(4), 2017- 
    11-07: Array(4), 2017-11-08: Array(4), …]

This all happens when jquery handles a click event.

图片转代码服务由CSDN问答提供 功能建议

我需要一些帮助,我的问题是第一个 console.log 返回 由 foreach 构建的,但后端没有收到相同的数组。 这个 foreach 基本上遍历一个表,查找某个id,使其成为一个数组的索引,然后从表中获取所有输入值并将其推送到该给定索引。

  var dateTime = []; 
 $(“#employeeData> tr”)。each(function(dateIndex,date){
 dateTime [date.id] = [  ]; 
 $(“#”+ date.id +“:input”)。each(function(inputIndex,inputTime){
 dateTime [date.id] .push($(inputTime).val())\  n}); 
}); 
 var CSRF_TOKEN = $('meta [name =“csrf-token”]')。attr('content'); 
 console.log(dateTime); 
 $。  post('employee',
 {
 date:dateTime,
 _token:CSRF_TOKEN 
},function(response){
 console.log(response); 
} 
); 
});  
 Route :: post('/ employee','EmployeesController @ updateRecords'); 
 
公共函数updateRecords(Request $ request)
 {
 返回$ request->日期; 
} 
 //这是dateTime的console.log,每个日期有4个输入
 //与之关联
 [2017-11-02:Array(4)  ,2017-11-03:Array(4),2017-11-06:Array(4),2017- 
 11-07:Array(4),2017-11-08:Array(4),...] \  n   
 
 

这一切都发生在jquery处理点击事件时。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duangenshi9836 2018-04-26 10:04
    已采纳

    It is not possible to pass an array as it is in the post, it should be converted.

    Just encode your array as JSON string and decode it in the backend.

    var dateTime = [];
        $("#employeeData > tr").each(function(dateIndex, date) {
            dateTime[date.id] = [];
            $("#" + date.id + " :input").each(function(inputIndex, inputTime) {
                dateTime[date.id].push($(inputTime).val())
            });
        });
        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        console.log(dateTime);
        var jsonString = JSON.stringify(dateTime);
        $.post('employee',
            {
                dates: jsonString,
                _token: CSRF_TOKEN
            }, function(response) {
                console.log(response);
            }
        );
        });
    

    in your PHP, decode the JSON string :

    $dates = json_decode(stripslashes($_POST['dates']));
    
    点赞 评论

相关推荐 更多相似问题