当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Advance Search using Dropdown in Laravel 5

问题描述:

I have a dropdown in dropdown four options are there and I'm trying to filter the data from database according to selected option, I have checked in console data is coming but in my view page data is not refreshing, I have attached image please check that, I'm Struggling please help.

This is my HTML Code view

right_side_bar latest image

                                             check this image for html view reference

 <select id="property_id" name="property_type" onchange="property_type(this.value)">

<option>Property Type</option>

<option value="Single Family Home">Single Family Home</option>

<option value="Multi_Family_Home">Multi Family Home</option>

<option value="Condominium">Condominium</option>

<option value="Townhome">Townhome</option>

</select>

this is my right side code

 @if (session('status'))

<div class="alert alert-danger">

{{ session('status') }}

</div>

@endif

@foreach($row as $val)

<div class="property-box">

<div class="property-box-inner">

<div class="property-box-picture">

<div class="property-box-price">

<a href="#">{{$val->state}}</a></div>

<div>

<a href="#" class="property-box-picture-target">

<img src="../images/test/{{$val->image}}" />

</a>

</div>

</div>

<div class="property-box-meta" id='basic-modal'>

<div class="property-box-meta-item col-xs-3 col-sm-3">

<strong>{{$val->property_room}}</strong>

<span>Bedroom</span>

</div>

<div class="property-box-meta-item col-xs-3 col-sm-3">

<strong>{{$val->property_baths}}</strong>

<span>Bath</span>

</div>

<div class="property-box-meta-item col-xs-3 col-sm-3">

<strong>{{$val->property_size}}</strong>

<span>Area</span>

</div>

<div class="property-box-meta-item col-xs-3 col-sm-3">

<strong>{{$val->property_baths}}</strong>

<span>Garages</span>

</div>

<a href="{{ URL::to('pages/property_details', array('id'=>$val->id)) }}" class='basic'>More Details</a>

</div>

</div>

</div>

@endforeach

</div>

This my Controller Code to fetch the Data from Database

 public function property_type()

{

$term=Input::get('ptyname');

$data = array();

$display = DB::table('property_details')

->where('property_type', 'LIKE', '%'.$term.'%')

->Where('sale_or_rent', '=', 'sale')

->get();

var_dump($display);

if(count($display)!=0)

{

return View::make('/pages/property_home', array('row'=>$display));

}

else

{

session::flash('status', 'No Records Found!!!');

return View::make('/pages/property_home', array('row'=>$display));

}

}

This is My Ajax to load the controller

 <script>

(function($) {

$('#property_id').on('change', function() {

var optionSelected = $(this).find("option:selected");

var prop_type = optionSelected.val();

$.ajax({

type: "GET",

url: "{{URL::to('/property_type') }}",

dataType: "json",

data: {ptyname: prop_type},

success:function(row)

{

$('#getRequestdata').html(row.html);

}

});

});

})(jQuery);

</script>

This is my Routes

 Route::get('/property_type', array('as' => 'property_type', 'uses' => '[email protected]_type'));

like this i am getting my data in console

array(2) {

[0]=>

object(stdClass)#293 (24) {

["id"]=>int(3)

["sale_or_rent"]=>string(4) "sale"

["property_type"]=>string(11) "Condominium"

["property_room"]=>string(1) "2"

["property_baths"]=> string(1) "2"

["property_size"]=>string(8) "200 sqft"

["property_garage"]=>string(1) "2"

["property_year"]=>string(4) "1959"

["property_floor"]=>string(1) "4"

["sale_by"]=>string(5) "owner"

["property_price"]=>string(7) "$200000"

["phonenumber"]=>string(10) "9941661138"

["address"]=>string(47) "610 Airport Rd, Nenana, AK 99760, United States"

["lat"]=>string(10) "64.5486956"

["log"]=>string(12) "-149.0927984"

["state"]=>string(6) "Alaska"

["city"]=>string(6) "Manely"

["zipcode"]=>string(6) "600097"

["description"]=>string(34) "this is my property ....contact me"

["image"]=>string(25) "2016-01-22-06-34-16-8.jpg"

["amenties"]=>string(63) "Air conditioning,Cleaning after exit,Dishwasher,Grill,Internet,"

["user_id"]=>string(0) ""

["created_at"]=>string(19) "2016-01-22 07:53:05"

["updated_at"]=>string(19) "2016-01-22 07:53:05"

}

}

网友答案:

you have to assign response to your view, you should create some div where you would append/replace your response

eg: in your view, create div below your select drop down, like <div id="search-response"> </div>

<script>
   (function($) {
       $('#property_id').on('change', function() {
            var optionSelected = $(this).find("option:selected");
             var prop_type = optionSelected.val();
        $.ajax({
               type: "GET",
               url: "{{URL::to('pages/property_type') }}",
               dataType: "json",
               data: {ptyname: prop_type},
               success:function(row)
            {
                alert(val);
                //update your code by this
                // this will assign response to div
                //<div id="search-response"> </div>
                $("#search-response").html().html(row);
            }
         }); 
     });
})(jQuery);
分享给朋友:
您可能感兴趣的文章:
随机阅读: