Aronaman
7 months ago
631
1
Laravel

ajax search

Posted 7 months ago by Aronaman

inside form tag i have a search field.

<div class="form-row ">

                     <div class="col-sm-6 col-md-2 col-lg-2 mb-4">
                        <div class="select-wrap">
                        
                          <input type="text" name="name" id="name" class="form-control" placeholder="Search By Name">
                         
                       </div>

                      </div>
  <div class="col-sm-6 col-md-2 col-lg-2 mb-4">
                        
                       <button id="search" class="btn btn-primary">Search</button>
                      </div> <!-- end of serach warap  -->
</div>

Ajax

function search(){
                          var form_data = {
                            search_name:$('#name').val(),
                            org_type:$('#type_of_organization').val(),
                            city:$('#city_id').val(),
                            type:$('#type').val(),
                            start_price:$('#amount_start').val(),
                            end_price:$('#amount_end').val(),
                            check_in:$('#check_in').val(),
                            check_out:$('#check_out').val(),
                          } //end of var

                        if(form_data){
                             $.ajax({
                                type:"GET",
                                url:"{{route('local.search')}}?search="+form_data,
                                dataType:'json',
                                data:form_data,
                                success:function(res){
console.log(res);
}

 function registorEvents(){
                    $(document).on('click','#search',search);
                }
                  registorEvents();

the problem is render blank page and change my url from ../book/create to../book

APIController

 public function localSearch(Request $request)
     { 
        

        if($request->ajax())
         {
             

                    $name=$request->get('name');
                    $org_type=$request->get('org_type');
                    $city=$request->get('city');
                    $type=$request->get('type');
                    $start_price=$request->get('start_price');
                    $end_price=$request->get('end_price');
                    $check_in=$request->get('check_in');
                    $check_out=$request->get('check_out');
.....

Route::get('api/local-search','[email protected]')->name('local.search');

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