0

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' => 'PageController@property_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" } }

1 Answers1

0

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);
Qazi
  • 5,015
  • 8
  • 42
  • 62
  • thanx for the reply ....according to my application when user searches any location then it redirect on map pages and shows result in map and right sidebar after that user want to filter the property on the basis of property type , property price etc so my question is how i will remove the existing div and show filtered result in my view.... –  Feb 24 '16 at 08:01
  • you can remove or set empty your div by class, id or tag eg `$('#id').remove();/empty()` or `$('.class').empty(); /remove();` – Qazi Feb 24 '16 at 08:04
  • I have tried but it's not returning response to my view page but in console i am getting the result and when i'm using $('.class').empty(); this code then div's are going permanently hidden... –  Feb 24 '16 at 09:28
  • `empty()` will remove your dom element contents, you have to assign it again, eg: you element class or id name is `ajax-content` so you can do this `$('#ajax-content').empty().html(row);` it will assign newly content to your div – Qazi Feb 24 '16 at 09:34
  • I am getting the data in console but this error is coming ReferenceError: property_type is not defined and where should i put this code $('#ajax-content').empty().html(row); i have placed in my on that page but it's not returning any result in view part –  Feb 24 '16 at 10:05
  • can you share the code of your right side view ? means where you want to append ajax response – Qazi Feb 24 '16 at 10:57
  • i have updated code for right side view please check that –  Feb 24 '16 at 12:05
  • can you tell me the line, where you want to show Ajax response data? – Qazi Feb 24 '16 at 12:17
  • within foreach loop i want to load my data just check @foreach($row as $val) starting and ending @endforeach loop just check the line after starting of foreach loop with in this class
    –  Feb 24 '16 at 12:27
  • I am supposing that you already generating your HTML from backend, and returning html in AJAX response, in your ajax function, where you wrote this `$("").html();` replace this line with this `$('.property-box').empty('').html(row);` this will assign ajax response data into `property-box` div – Qazi Feb 25 '16 at 03:57
  • can you show me your ajax response, show completely – Qazi Feb 25 '16 at 06:45
  • I am asking about console response – Qazi Feb 25 '16 at 06:52
  • suppose I am searching for a property through drop down for that property two records are there in my database and i am getting response in my console like this –  Feb 25 '16 at 06:54
  • i have console data above check that i'm using var_dump() to check whether i am getting data or not.. –  Feb 25 '16 at 07:08
  • sorry for the delay i am new to stack overflow that's why taking much time for formatting my code please suggest something.... –  Feb 25 '16 at 07:52
  • comment out your `var_dump()` line, and add `->render();` with `View::make()` eg look this. `return View::make('/pages/property_home', array('row'=>$display))->render();` this will render/prepare your HTML for ajax response – Qazi Feb 25 '16 at 10:22
  • can you tell me now what response you are getting ? – Qazi Feb 25 '16 at 11:27
  • i am not getting anything but when i am using var_dump i can see the output in console response window and in html window i can see the results –  Feb 25 '16 at 12:09
  • its mean that you are getting the DB records, but not View HTML, just try something custom html and check in console. before `if()` statement write this eg: `return '

    Test me here

    ';`
    – Qazi Feb 25 '16 at 12:15
  • In my controller i have written your thing return '

    Test me here

    '; but on view page it's not returning anything i also have tried like this
    and in ajax like this $('.result').append(data);
    –  Feb 25 '16 at 12:22
  • http://localhost/realestate-site/public/property_type?ptyname=Condominium when i am opening the url in new tab like this i can see the result –  Feb 25 '16 at 12:28
  • can you zip your files and send it to me ? So I can have a look? – Qazi Feb 25 '16 at 13:02
  • you can attach here, just give me required files, not all – Qazi Feb 25 '16 at 13:58
  • right now i'm leaving i will update my code after reaching my home –  Feb 25 '16 at 14:39
  • upload it somewhere else(some free storage area or google drive), and share the link here – Qazi Feb 26 '16 at 06:07
  • check this link https://drive.google.com/folderview?id=0B6ukGlAs8hDvTEZPUmxqcGtyWWM&usp=sharing –  Feb 26 '16 at 06:31
  • hey man in my controller i have tried like this $returnHTML = view('/pages/property_home')->with('row', $display)->render(); return response()->json(array('success' => true, 'html'=>$returnHTML)); after that in my ajax i have tried to alert the value like this alert(row); i am getting [object object] in my alert. –  Feb 26 '16 at 14:18
  • in your `alert` just type this `alert(row.html)` I hope you will get the response – Qazi Feb 29 '16 at 03:16
  • Hi I'm getting the result but it's not returning in correct format, I'm getting my whole webpage with results but i'm only want results not whole web page, so how i can send only html view of filtered things... and thanxx for your reply –  Feb 29 '16 at 06:03
  • actually you are returning overall view `view('/pages/property_home')` So you have to return specific view, eg copy your right side content area, and create a new view `rightsidearea.blade.php` and paste it in it. save it, and now in your controller method, do this `view('/pages/rightsidearea')....` you will get specific result – Qazi Feb 29 '16 at 06:43
  • Hi i'am sorry need one more help ....onchange I'm getting the results from DB but old record is not going, idea is to remove the old record's and show filtered results. thanxx –  Mar 02 '16 at 09:12
  • which old record you want to remove ? still that right sidebar one ? – Qazi Mar 02 '16 at 09:21
  • yes, On homepage one searchbox is there when user is searching any city/state according to that i'm showing the results on nextpage in right sidebar after that user can filter the data usnig advance search but when i'm filtering old record is not going whatever record i had got from homepage search. –  Mar 02 '16 at 09:32
  • can you show me your latest ajax code, which you are using to send request and update results ? – Qazi Mar 02 '16 at 09:51
  • also share your right side html structure, and where is this element `getRequestdata`? actually you have to set empty your old results 1st then you have to assign new results. eg if your old results exists in this div `getRequestdata` then you have to do this `$('#getRequestdata').html('').html(row.html)` – Qazi Mar 02 '16 at 10:43
  • thanx man now it's working before that i was not returning the output in that div –  Mar 02 '16 at 11:49
  • Hi thanx man my query is when user selects a dropdown ,i want to load both the things at the same time in google map with filtered result and right side content.Now right side filtering is working fine –  Mar 07 '16 at 06:49
  • Please mark my answer as correct one, and create a new question for your this problem, with screen shots and details. So I will answer it there. actually these comments getting too long, so avoid this. and create new one – Qazi Mar 07 '16 at 06:53
  • I have already marked your question as correct, I think my reputaion is below 15 that's why it;s not showing here is my thread http://stackoverflow.com/questions/35837349/on-change-load-google-map-and-div-from-database-laravel5 –  Mar 07 '16 at 07:00