【问题标题】:How to get checkbox checked from database array using Laravel and Ajax如何使用 Laravel 和 Ajax 从数据库数组中检查复选框
【发布时间】:2019-08-16 14:36:06
【问题描述】:

我正在尝试将数据从数据库获取到选中的复选框。数据库中的复选框数据是以json形式插入的数组,是基于insert函数的动态数据。

我的任务表:

id |employee_id | startDate  | endDate   | ...
---|------------|------------|-----------|--------
1  |["1","2"]   | .......... | ..........| ....

我的TasksController.php

function fetchdata(Request $request)
    {
        $id = $request->input('id');
        $task = Task::find($id);
        $output = array(
            'employee_id'    =>  $task->employee_id,
            'name'    =>  $task->name,
            'description'    =>  $task->description,
            'startDate'    =>  $task->startDate,
            'endDate'    =>  $task->endDate,
            'percentage'    =>  $task->percentage       

        );
        echo json_encode($output);
    }

    public function getEmployeesList()
    {

        $employeesList = Employee::all();


        return view('adminlte::tasks', ['employeesList' => $employeesList]);


    }

我的tasks.blade.php

 @foreach($employeesList as $emplist)
    <label class="checkbox-inline">
      <input type="checkbox" id="employee_id" name="employee_id[]" class="chck" value="{{$emplist->id}}" >{{ $emplist->name }}</label>    
 @endforeach

我在刀片中的 Ajax 函数:

$(document).on('click', '.edit', function(){
        var id = $(this).attr("id");
        $('#form_output').html('');
        $.ajax({
            url: "{{route('tasks.fetchdata')}}",
            method: 'get',
            data: {id:id},
            dataType: 'json',
            success:function(data)
            {
                $('#id').val(data.id);
                $('#employee_id').val(data.employee_id);
                $('#name').val(data.name);
                .......................
                ..................
            }
        })
    });

那么,我如何将数据从数据库检索到选中的复选框,因为现在我在尝试更新记录时得到空的“employee_id”值。

提前谢谢你

【问题讨论】:

    标签: arrays json ajax laravel checkbox


    【解决方案1】:

    由于您在服务器端对数据进行编码,因此您必须在客户端对其进行解码,例如:

    ...
    success:function(data)
    {
        console.log( data );
        data = JSON.parse(data);
    
        $('#id').val(data.id);
        $('#employee_id').val(data.employee_id);
        $('#name').val(data.name);
        ...
    
    }
    

    【讨论】:

    • 我收到一个错误:Uncaught SyntaxError: Unexpected token o in JSON at position 1. 我也尝试了 data = JSON.parse(JSON.stringify(data)),没有收到错误但什么也没有发生在复选框中。
    • echo json_encode($output); 更改为 return json_encode($output); 然后用 console.log( data ); 检查我的更新答案,试试看,然后向我们展示输出。
    • 控制台日志:{employee_id: "["1","2"]", name: "test", description: "test", startDate: "2019/03/25 15:44", endDate: "2019/03/26 15:44", …} 也是同样的错误。
    • 问题来自 PHP 端数组,所以尝试dd( json_encode($output) ); 并通过添加完整输出来编辑您的问题。
    • dd( json_encode($output) ); 不工作,我什么也没得到
    猜你喜欢
    • 1970-01-01
    • 2020-06-04
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    相关资源
    最近更新 更多