skip to Main Content

when i try to search data from contactTable i cant see anything
i post also the MessageController because this works so,i think i make some confusion and i have double something in some part of code,

enter image description here

enter image description here

SearchMessageController–>

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use DB;
class SearchMessageController extends Controller
{
   public function index()
{
    return view('searchMessage.searchMessage');
}
    public function search(Request $request)
{
    if($request->ajax())
{
    $output="";
    $sms_histories=DB::table('sms_histories')->where('phone_number','LIKE','%'.$request->search."%")->get();
    if($sms_histories)
{
    foreach ($sms_histories as $key => $sms_histories) {
    $output.='<tr>'.
    '<td>'.$sms_histories->id.'</td>'.
    '<td>'.$sms_histories->phone_number.'</td>'.
    '<td>'.$sms_histories->message.'</td>'.
    '</tr>';
}
    return Response($output);
   }
}
}
}

this is my view for the message:



<!DOCTYPE html>
<html>
    <head>
    <meta name="_token" content="{{ csrf_token() }}">
    <title>Live Search</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="panel panel-default">
                    <div class="panel-heading">
                    <h3>Suche nach Telefonnummern </h3>
                    <div class="panel-heading"><a href="{{ url('home') }}">zurück zum Senden von Nachrichten</a></div>
                    </div>
                <div class="panel-body">
            <div class="form-group">
            <input type="text" class="form-controller" id="search" name="search"></input>
        </div>
            <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th>NUMMER</th>
                <th>NACHRICHT</th>
            </tr>
        </thead>
    <tbody>
    </tbody>
    </table>
    </div>
        </div>
            </div>
                </div>
<script type="text/javascript">
$('#search').on('keyup',function(){
$value=$(this).val();
$.ajax({
type : 'get',
url : '{{URL::to('search')}}',
data:{'search':$value},
success:function(data){
$('tbody').html(data);
}
});
})
</script>
<script type="text/javascript">
$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>
</body>
</html>

that’s my ContactController

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use DB;
class SearchContactController extends Controller
{
   public function index()
{
    return view('searchContact.searchContact');
}
    public function searchContact(Request $request)
{
    if($request->ajax())
{
    $output="";
    $contact=DB::table('contact')->where('lastName','LIKE','%'.$request->search."%")->get();
    if($contact)
{
    foreach ($contact as $key => $contact) {
    $output.='<tr>'.
    '<td>'.$contact->id.'</td>'.
    '<td>'.$contact->firstName.'</td>'.
    '<td>'.$contact->lastName.'</td>'.
    '<td>'.$contact->occupation.'</td>'.
    '<td>'.$contact->birthday.'</td>'.
    '<td>'.$contact->phone_number.'</td>'.
    '</tr>';
}
    return Response($output);
   }
}
}
}

Contact view



<!DOCTYPE html>
<html>
    <head>
    <meta name="_token" content="{{ csrf_token() }}">
    <title>Live Search</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="panel panel-default">
                    <div class="panel-heading">
                    <h3>Kontaktliste </h3>
                    <div class="panel-heading"><a href="{{ url('home') }}">zurück zum Senden von Nachrichten</a></div>
                    </div>
                <div class="panel-body">
            <div class="form-group">
            <input type="text" class="form-controller" id="searchContact" name="search"></input>
        </div>
            <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>firstName</th>
                <th>lastName</th>
                <th>occupation</th>
                <th>birthday</th>
                <th>phone_number</th>
            </tr>
        </thead>
    <tbody>
    </tbody>
    </table>
    </div>
        </div>
            </div>
                </div>
<script type="text/javascript">
$('#search').on('keyup',function(){
$value=$(this).val();
$.ajax({
type : 'get',
url : '{{URL::to('search')}}',
data:{'search':$value},
success:function(data){
$('tbody').html(data);
}
});
})
</script>
<script type="text/javascript">
$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>
</body>
</html>

and that’s are my routes:

Route::group(['middleware' => 'auth'], function () {
    Route::get('/searchMessage.searchMessage','SearchMessageController@index');
    Route::get('/search','SearchMessageController@search');
    Route::get('/searchContact.searchContact','SearchContactController@index');
    Route::get('/searchContact','SearchContactController@search');
    
});

if you have an idea thx

i try to change the id in the views fromn search to searchContact, i think i made some confusion and now if i put id=search the Contactlist show the field from the messageTable..

2

Answers


  1. In ajax of contact view, it should be:

    '{{URL::to('searchContact')}}'
    

    It should be better if you name the route, such as:

     Route::get('/searchContact','SearchContactController@search')->name('contact.search');
    

    Then, in ajax:

    url: '{{route('contact.search')}}'
    
    Login or Signup to reply.
  2. {{URL::to('search')}} this is the error you have made. Try to change this into
    {{URL::to('searchContact')}}. ajax will sent request to serachContact url

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search