nafeeur10
1 year ago
105
2
Laravel

Error to pass Dropdown value in View to Controller (ajax Call)

Posted 1 year ago by nafeeur10

I have two select option named: Category and Price range. While I will press a button these two values will go to my Controller.

But there is an error in Count function. Error: "count(): Parameter must be an array or an object that implements Countable",??

My ajax code:

$("#findBtn").click(function()
        {
            var cat = $("#catID").val();
            var price = $('#priceID').val();

            $.ajax({
                type: 'get',
                dataType: 'html',
                url: '{{url('/productsCat')}}',
                data: 'cat_id=' + cat + '&price=' + price,
                success:function(response)
                {
                    console.log(response);
                    $("#productData").html(response);
                }
            });
        });```

Blade:

                        <label>Product Category</label>
                        <select id="catID" class="form-control">
                            <option value="">Select a Category</option>
                                @foreach(App\Category::all() as $cList)
                                    <option id="cat{{ $cList->id }}" class="option" value="{{$cList->id}}">{{$cList->cat_name}}</option>
                                @endforeach
                        </select>

                    </div>
                    <div class="col-xs-6 col-sm-3 form-group col-md-4">
                        <label>Product Price Range</label>
                        <select id="priceID" class="form-control">
                            <option value="">Select Price Range</option>
                            <option value="0-100">0-100</option>
                            <option value="100-300">100-300</option>
                            <option value="300-500">300-500</option>
                            <option value="500-1000">500-1000</option>
                        </select>
                    </div>


                <div class="col-sm-6 hidden-xs col-md-4">
                    <div class="row">

                        <div class="col-sm-4 pull-right">
                            <button id="findBtn" class="btn btn-success">Find</button>
                        </div>
```

Controller:

public function productsCat(Request $request)
    {
        $search_cat = $request->cat_id;
        $priceCount = count($request->price);


        if($search_cat!="" && $priceCount!="0")
        {
            echo "Both are selected!";
        }

        else if($priceCount!="0")
        {
            echo "Only Price has selected!";
        }

        else if($search_cat!="")
        {
           echo "Cat is Selected!";
        }

        else
        {
            echo "<h1 style='text-align: center'>Please select atleast one Filter.</h1>";
        }
}```

Please sign in or create an account to participate in this conversation.