如何使用Ajax和多个复选框从控制器获取结果

时间:2019-06-17 03:51:14

标签: php jquery ajax laravel

我如何使用复选框的值数组从ajax获取结果,我是Laravel和ajax的新手,我试图制作一个过滤器以使用复选框显示一些数据,在这段代码中,我试图实现它仅复选框的值而不是显示控制器的结果,请在ajax和控制器中需要帮助以解决此问题 我是从另一个关于stackoverflow的问题中获得此代码的,但是缺少一些东西,我不知道它到底在哪里

刀片

@foreach
    <div class="custom-radio">
        <input type="checkbox"  class="batchCheckbox" name="batch[]" value=" {{$item->indivgroup_option}}">
        <label>
            <span>
                {{ ($item->indivgroup_option =='i')?"Individual Tour":"Group Tour" }}
            </span>
        </label>
    </div>
@endforeach  

jQuery

var categories;
var getfilters = function () {
    $.ajax({
        type: 'POST',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url:'{{URL::to('offer-category')}}',
        data: {categories: categories},
        error: function(e) {
            console.log(e.responseText);
        },
        success: function (data) {
            console.log(data);
            $('#myResponse').html(data);
        }
    });
};


$(document).ready(function() {
    $('input[type="checkbox"]').on('change', function(event) {
        event.preventDefault();
        categories = $("input:checkbox:checked").map(function(){
            return $(this).val();

        }).get();

        $('#myResponse').html(categories.toString());
        getfilters();
    });
});

控制器

public function getoffergroup(Request $request)
{

    $input  = array(Input::get('categories'));
    $posts=Offer::whereIn('indivgroup_option' , $input)->get();
    $categories = Category::getGroup();
    return response()->view('offer.archive', compact('offers', 'categories'));
}

1 个答案:

答案 0 :(得分:0)

您好,请尝试通过复选框获得此值,希望对您有所帮助

<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<input type="checkbox" name="batch" value="1">
<input type="checkbox" name="batch" value="2">
<input type="checkbox" name="batch" value="3">
<input type="checkbox" name="batch" value="4">
<input type="checkbox" name="batch" value="5">

<script type="text/javascript">
    var categories = [];
    var getfilters = function () {
        console.log(categories);
  $.ajax({
            type: 'POST',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },

            url:'{{URL::to('offer-category')}}',

            data: {categories: categories},
            error: function(e) {
                console.log(e.responseText);
            },
            success: function (data) {
                console.log(data);
                $('#myResponse').html(data);
        }});
    };


    $(document).ready(function() {
        $('input[type="checkbox"]').on('click', function(event) {
           event.preventDefault();
            $.each($("input[name='batch']:checked"), function(){            
                categories.push($(this).val());
            });

        $('#myResponse').html(); // <---- 'one,two,three;
             getfilters();
        });
    });
</script>

----------控制器获取数据并将数据返回给ajax -----

    public function getoffergroup(Request $request)
{
    $categories = $request->categories; // this is use for get request, if you are getting single value make lopp for single like :

    $posts = Offer::whereIn('indivgroup_option' , $categories)->get();

    // if you resturn result in ajax use this for 

    $categories = Category::getGroup();

    if (!empty($posts)) {
        return response()->json([
            'status'=>200,
            'message'=>"sussfully get data",
            "data" =>$posts // if you return any data in ajax use this 
            "key"=>"value" // value mean data  
        ]);
    }else{
        return response()->json([
            'status'=>404,
            'message' => "no data found"
        ]);
    }
}