php - Laravel Ajax Search -
i trying create search form, searches record in database table while user typing search field.
controller
public function search() { // search term $term = input::get('term'); // search members table $members = db::table('members') ->where(concat("members.member_first_name,' ',members.member_last_name"), 'like', "$term") ->orwhere(concat("members.member_last_name,' ',members.member_first_name"), 'like', "$term") ->orwhere(concat("members.member_name_affix,' ',members.member_last_name,' ',members.member_first_name"), 'like', "$term") ->orwhere(concat("members.member_first_name,' ',members.member_name_affix,' ',members.member_last_name"), 'like', "$term") ->orwhere('members.member_first_name', 'like', "$term") ->orwhere('members.member_last_name', 'like', "$term") ->orwhere('members.member_business_address', 'like', "$term") ->get(); // return results in json format return response()->json($members); }
route
route::get('search', 'mitgliederverzeichniscontroller@search');
view
<!-- search form --> <form method="get" id="search-form"> <input type="search" id="term" name="term" onkeyup="ajaxsearch()" placeholder="search members table..." autofocus> </form> <!-- ajax function --> <script> function ajaxsearch() { $.ajax({ url: 'search', type: 'get', data: {term: $('#term').val()}, datatype: 'json', success: function(response) { console.log(data); } }); } </script> <!-- results table --> <div class="clear"></div> <div id="resulttablewrapper"> <table id="resulttable"> <thead> <tr> <th>name</th> <th>address</th> <th>business address</th> <th>join date</th> </tr> </thead> <tbody></tbody> </table> </div><!-- /#resulttablewrapper --> <div class="clear"></div> <p id="search-info">please type search phrase in field above.</p> <!-- use json response fill results table --> @if (isset($_get['term'])) @foreach ($members $member) <tr> <td> <strong> {!! $member->member_name_affix.' '.$member->member_last_name.' '.$member->member_first_name.' '.$member->member_title !!} </strong><br><br> <div class="unhighlight"> {!! $member->member_business_position !!}<br><br> {!! $member->member_business_name !!} </div> </td> <td class='unhighlight'> @if ($member->member_private_address) <span> {!! $member->member_private_address !!} <br> {!! $member->member_private_zip_code. ' ' .$member->member_private_location !!} </span><br> <div class='clear'></div> @endif </td> ... , on ... </tr> @endforeach @endif
error message getting when start typing letter 'a' search field:
get http://localhost.local/search?term=a 500 (internal server error)
new laravel , thankful kind of help!
update
amieiro managed figure out error seems input::get() method:
you can inspect error trying see json. in google chrome can see inspecting element, reloading page , in network tab, xhr filter. can see in next image
Comments
Post a Comment