首页 > 解决方案 > Laravel:使用 Ajax 和 Jquery 从选择选项中获取值

问题描述

我对我的代码有疑问,我有模块,管理员可以过滤用户订购的内容。场景管理员有/有一个订购的客户列表,管理员将选择哪个客户前。客户 2所以现在 url http://localhost:8000/customer_all_orders/2 注意:/2 是 customer_id 有 4 个订单的记录,

所以现在管理员可以过滤记录,如果管理员选择下拉3个订单查询将限制3个,也可以限制15个订单等。

问题:如果我在选择选项上选择了一些值,为什么订单不能限制订单?但状态码是(OK)

结果在这里: 结果控制台


我的脚本/控制器:

public function customer_all_orders($id,Request $request) {

    $filter = $request->get('filter');
    if(isset($filter))
    {
        $customer_details_id = DB::select('SELECT * FROM customer_details WHERE customer_id = ? ',[$id]);

        $select_order_properties = DB::select('SELECT * FROM order_properties WHERE customer_id = ? ORDER BY order_id DESC LIMIT '.$filter.' ',[$id]);

        $select_order_details = DB::select('SELECT order_properties_id,menu_cat_image,or_number,Quantity,Subtotal,menu_cat_name FROM order_properties as op LEFT JOIN (SELECT order_properties_id,product_id,UnitPrice,Quantity,(UnitPrice * Quantity) as Subtotal FROM order_details_properties) odp ON op.order_id = odp.order_properties_id 
        LEFT JOIN (SELECT menu_cat_image,menu_cat_name,menu_cat_id FROM menu_category) mc ON odp.product_id = mc.menu_cat_id
        WHERE customer_id = ?  ',[
            $id
        ]);

        return View('customer_all_orders')
        ->with('customer_details',$customer_details_id)
        ->with('order_properties',$select_order_properties)
        ->with('customer_order',$select_order_details);
    }
    else
    {
        $customer_details_id = DB::select('SELECT * FROM customer_details WHERE customer_id = ? ',[$id]);

        $select_order_properties = DB::select('SELECT * FROM order_properties WHERE customer_id = ? ORDER BY order_id DESC LIMIT 4',[$id]);

        $select_order_details = DB::select('SELECT order_properties_id,menu_cat_image,or_number,Quantity,Subtotal,menu_cat_name FROM order_properties as op LEFT JOIN (SELECT order_properties_id,product_id,UnitPrice,Quantity,(UnitPrice * Quantity) as Subtotal FROM order_details_properties) odp ON op.order_id = odp.order_properties_id 
        LEFT JOIN (SELECT menu_cat_image,menu_cat_name,menu_cat_id FROM menu_category) mc ON odp.product_id = mc.menu_cat_id
        WHERE customer_id = ?  ',[
            $id
        ]);


        return View('customer_all_orders')
        ->with('customer_details',$customer_details_id)
        ->with('order_properties',$select_order_properties)
        ->with('customer_order',$select_order_details);
    }


}

我的 Ajax 请求:

$(document).ready(function(){
                $('#filter_orders').on('change',function(){
                    var value = this.value;
                    var customer_id = $(this).find(':selected').attr('data-user-id');

                    $.ajax({
                        url:'/customer_all_orders/' + customer_id,
                        type:'get',
                        data:{filter:value},
                        success:function(response){
                            console.log(response);
                        },
                        error: function(error){
                            console.log(error);
                        }
                    })


                });
            })

我的HTML:

<select class="form-control" id="filter_orders" data-user-id='{{$details->customer_id}}'>
                <option value="" selected="">Select Filter Orders</option>
                <option value="3" data-user-id='{{$details->customer_id}}'>Last 3 Orders</option>
                <option value="15" data-user-id='{{$details->customer_id}}'>Last 15 Orders</option>
                <option value="*" data-user-id='{{$details->customer_id}}'>Show all Orders</option>
            </select>

标签: ajaxlaravel

解决方案


推荐阅读