首页 > 解决方案 > 如何将控制台日志结果传递给我的 ajax,以便我可以将其插入 DB?

问题描述

我正在做一个配对系统。我的问题是,如何将我的 console.log 结果插入我的数据库?当我的数据库中有相同级别时,console.log 的结果就会发生。

, 例如,测试 4的级别为 1测试 1 的级别为 1它们将加入 1 个数组,因为它们具有相同的级别

其他人也一样。我在下面提供了我的代码和目标的屏幕截图。任何帮助将不胜感激。非常感谢。

在此处输入图像描述

ajax 获取:

<script>

  

    let ajaxResult = []; // the pushed data will be saved here
    let table;
    let base_url = "<?php echo base_url();?>";
    let result = [];
    
    const combine = (source) => {
    
        return source[0].data.reduce((acc, curr) => {
            const [entryID,  entryName, level] = curr;
            if (acc[level])
                acc[level].push({
                    entryID,
                
                    entryName,
                    level
                });
            else
                acc[level] = [{
                    entryID,
                  
                    entryName,
                    level
                }];
            return acc;
        }, {})
    }
    
    
    $(document).ready(function() {
        //datatables
        table = $("#entry_list1").DataTable({
    
    
            processing: false,
            serverSide: true,
            order: [],
            searching: false,
            paging: false,
            info: false,
    
            ajax: {
                url: "<?php echo site_url('controller/fetch_players')?>",
                type: "POST",
                async: true,
                dataType: "json",
    
                success: function(data) {
    
                    ajaxResult.push(data); // I'm pushing my data to my ajaxResult variable
                    result = combine(ajaxResult); // Cleanup your data here. 
                    console.log(combine(ajaxResult)); // The result in image i provided above is from here.
    
                },
            },
    
            "columnDefs": [{
                    "targets": [0], //first column
                    "orderable": false, //set not orderable
                },
                {
                    "targets": [-1], //last column
                    "orderable": false, //set not orderable
                },
    
            ],
        });
    });
      
      </script>

阿贾克斯插入:

// i want to pass my ajaxResult = []; here so that i can insert it into my database


    <script type="text/javascript">
    
        $("#insertMe").submit(function(event) {
        
        var form = $('#insertMe')[0];
        var formData = new FormData(form);
            event.preventDefault();
            $.ajax({
                    url: "<?php echo base_url('controller/insertFunction'); ?>",
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: formData,
                    type: "post",
                    async: false,
                    dataType: 'json',
                    success: function(data){
                      
                     
            
                    },
      
          });
          
        });
    
    </script>

控制器:

function inserFunction() {

 $this->db->insert('matching', $data);   

}

标签: javascriptajaxcodeignitercodeigniter-3

解决方案


推荐阅读