Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用ajax和laravel进行搜索_Jquery_Ajax_Laravel_Laravel 5.5_Laravel 5.6 - Fatal编程技术网

Jquery 使用ajax和laravel进行搜索

Jquery 使用ajax和laravel进行搜索,jquery,ajax,laravel,laravel-5.5,laravel-5.6,Jquery,Ajax,Laravel,Laravel 5.5,Laravel 5.6,我有一张叫做缩进的桌子。我需要通过输入客户名称来搜索和获取详细信息。 当我按下一个按钮时,它应该在datatable中返回客户详细信息,我使用的是ajax和laravel。当它工作时,它只会在我尝试搜索名字时返回最后的数据 索引文件 <script> $(document).ready(function () { $('#search_data').on('click', function () {

我有一张叫做缩进的桌子。我需要通过输入客户名称来搜索和获取详细信息。 当我按下一个按钮时,它应该在datatable中返回客户详细信息,我使用的是ajax和laravel。当它工作时,它只会在我尝试搜索名字时返回最后的数据

索引文件

       <script>

        $(document).ready(function () {
            $('#search_data').on('click', function () {
                $value = $(this).val();
                $.ajax({
                    type: 'post',
                    url: '{{\Illuminate\Support\Facades\URL::to('search')}}',
                    data: {'search': $value},
                    success: function (data) {
                        $('#edpinfo').html(data);

                    }
                })

            })
        })
    </script>
    <script>
        $.ajaxSetup({headers: {'csrftoken': '{{ csrf_token() }}'}});
    </script>
    <br>
    <div class="row">
        <div class="col-12">
            <div class="card m-b-30">
                <div class="card-body">

                    <h4 class="mt-0 header-title">Indents</h4>

                    <input type="text" id="search" class="pull-right form-rounded">
                    <a href="{{route('edp')}}">
                        <button class="btn btn-sm btn-primary pull-left">Back</button>
                    </a>
                    <button id="search_data" class="btn btn-danger btn-sm pull-right">search</button>

                    <br>
                    <br><br>
                    <table id="datatable" class="table table-bordered  table-responsive-lg">
                        <thead>
                        <tr>
                            <th>Slno</th>
                            <th>Customer Name</th>
                            <th>Customer Phone Number</th>
                            <th>DateOfDelivery</th>
                            <th>Delivery At</th>
                            <th>Show</th>

                        </tr>
                        </thead>
  <tbody id="edpinfo">
                    @foreach($indents as $indent)
                        <tr>
                            <td>{{$loop->iteration}}</td>
                            <td>{{$indent->customer_name}}</td>
                            <td>{{$indent->phone_no}}</td>
                            <td>{{$indent->date_of_delivery}}</td>
                            <td>{{$indent->delivery_at}}</td>
                            <td><a href="{{route('edp.show',$indent->id)}}"><img src="assets/images/select.jpg"
                                                                                 class="imgsize"></a></td>

                        </tr>
                    @endforeach
                    </tbody>
                </table>
        </div>
    </div>
    </div>

$(文档).ready(函数(){
$('search#u data')。在('click',函数(){
$value=$(this.val();
$.ajax({
键入:“post”,
url:“{\illumb\Support\Facades\url::to('search')}”,
数据:{'search':$value},
成功:功能(数据){
$('#edpinfo').html(数据);
}
})
})
})
$.ajaxSetup({headers:{'csrftoken':{{{{csrf_token()}}}}});

缩进 搜索


斯尔诺 客户名称 客户电话号码 交货日期 交货地点 显示 @foreach($indents作为$indent) {{$loop->iteration} {{$indent->customer_name} {{$indent->电话号码} {{$indent->交货日期} {{$indent->delivery_at} @endforeach
控制器文件

    public function search(Request $request)
    {

        if ($request->ajax()) {

            $output = "";
            $indents = indents::table('indents')
                ->where('customer_name', 'LIKE', '%' . $request->search . '%')->get();
            foreach ($indents as $key => $indent) {

                        $output = '<tr>' .
                        '<td>' . $indent->id . '</td>' .
                        '<td>' . $indent->customer_name . '</td>' .
                        '<td>' . $indent->phone_no . '</td>' .
                        '<td>' . $indent->date_of_delivery . '</td>' .
                        '<td>' . $indent->delivery_at . '</td>' .
//                        '<td>'.'.<a href="{{route(\'edp.show\',$indent->id)}}">.'.'<img src="assets/images/select.jpg" class="imgsize">.'.'</a>.'.'</td>'.
                        '</tr>';

            }
            return Response($output);
        }

    }
公共功能搜索(请求$Request)
{
如果($request->ajax()){
$output=“”;
$indents=indents::表('indents')
->其中('customer_name','LIKE','%.$request->search'%')->get();
foreach($key=>$indent的缩进){
$output=''。
'.$indent->id'.'。
''.$indent->客户名称''。
''.$indent->电话号码''。
''.$indent->交货日期''。
''.$indent->交货地点''。
//                        ''.'..'.''.
'';
}
返回响应(输出);
}
}

您的问题是总是覆盖
$output
变量

因此,不是
$output=''


$output.=''
(注意点)

我刚按你说的做了,然后运行程序,并用chrome web工具检查它显示POST 405(不允许使用方法)你正在发布到一个没有为POST请求定义的路由,检查你的路由,并更新
搜索
路由到POST,或者更新您的ajax脚本以使用GETohh s**t我已经更改了ajax get to post,但我没有更改路由文件。我的问题,谢谢您如果现在输出良好,您介意接受我的回答吗?