user7424490
user7424490

Reputation:

How to check if user has selected multiple checkboxes

I am working on a Laravel 5.8 project which is an Online Store. and in this project, I wanted to add "Printing Order Factors" feature for Admins.

So I have made a form like this:

<form method="POST" action="{{ route('orders.newprint') }}">
    @csrf
    @forelse($orders as $order)
    <tr>
        <td><input class="form-check-input" name="orderCheck[]" type="checkbox" value="{{ $order->ord_id }}">&nbsp;</td>
        <td>{{ $order->ord_id }}</td>
        <td>{{ $order->status_label }}</td>
        <td>{{ $order->customer_name }}</td>
        <td>{{ $order->ord_total }}</td>
    </tr>
    @empty
        <td colspan="7" class="text-center">No order to show</td>
    @endforelse
</form>

So basically admins can select multiple orders like this:

enter image description here

And I'm trying to send order ids as an array:

<input class="form-check-input" name="orderCheck[]" type="checkbox" value="{{ $order->ord_id }}">

Now at the Controller, I need to check if user has selected multiple orders or not

public function prnpriview(Request $request)
    {
        if(!empty($request->input('actions'))){
            if($request->input('actions')=='print_factors'){
                if(USER SELECTES MORE THAN ONE CHECKBOX) { 
                    ...
                    return view('admin.shop.orders.newprint', compact('args'));
                }else{
                    return back();
                }
            }
        }else{
            return back();
        }
    }

So how to check if user has selected multiple checkboxes (more than one checkbox) or not ?

I would really appreciate any idea or suggestion from you guys about this...

Thanks.

Upvotes: 2

Views: 736

Answers (1)

Yasin Patel
Yasin Patel

Reputation: 5731

$request->orderCheck will be in an array, so you can use count()

$checked = $request->input('orderCheck',[]);// if checkbox is not checked than return empty array
if(count($checked) > 1) { 
    return view('admin.shop.orders.newprint', compact('args'));
}else{
    return back();

Upvotes: 3

Related Questions