Laravel:使用Ajax和Jquery从选择选项中获取值

时间:2019-01-20 14:21:09

标签: ajax laravel

我对自己的代码有疑问,我有一个模块,管理员可以过滤用户订购的内容。场景管理员具有/已订购的客户列表,管理员将选择哪个客户Ex。 客户2 ,因此,网址http://localhost:8000/customer_all_orders/2 请注意:/ 2是customer_id 记录了4个订单,

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

  • 3个订单
  • 15个订单
  • 显示所有订单

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

结果在这里: Result Console


我的脚本/控制器:

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>

0 个答案:

没有答案