0

Hello i am developping a login application with live search in laravel with ajax and Mysql, but i keep getting an empty tableview when i try to display my database table. After some debbuging i found that the program stop in if($request->ajax()) so there is no communication inside if($request->ajax()) brackets.

There is my code:

View:

<body>
    <br />
    <div class="container box">
        <h3 align="center">Recherche de PDR</h3>
        <br /> @if(isset(Auth::user()->email))
        ....    
        <br />

        <div class="panel panel-default">
            <div class="panel-heading">Recherche de PDR</div>
            <div class="panel-body">
                <input type="text" name="search" id="search" class="form-control" placeholder="Recherche PDR" />
                <div class="table-responsive">
                    <h3 align="center">Total PDR : <span id="total_records"></span></h3>
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>Pièce</th>
                                <th>ID-Pièce</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

<script>
    $(document).ready(function(){

        fetch_pdr_data();

       function fetch_pdr_data(query = '')
       {
            $.ajax({
                url:"{{ route('recherche.action') }}",
                method:'GET',
                data:{query:query},
                dataType:'json'
                success:function(data)
                {
                    $('tbody').html(data.table_data);
                    $('#total_records').text(data.total_data);
                }
            })
       }

       $(document).on('keyup', '#search', function(){
            var query = $(this).val();
            fetch_pdr_data(query);
       });

    });

</script>

Controller:

<?php

    namespace App\Http\Controllers;
    use App\Pdrs;
    use DB;

    use Illuminate\Http\Request;

    class PDRController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            return view('recherche.index');
        }

        function action(Request $request)
        {//working
            if($request->ajax())
            {//not working
                $query = $request->get('query');
                if($query != '')
                {
                    $data = DB::table('pdrs')
                            ->where('Désignation', 'like', '%'.$query.'%')
                            ->orWhere('Status', 'like', '%'.$query.'%')
                            ->get();
                }
                else
                {
                    $data = DB::table('pdrs')
                            ->orderBy('ID-Pièce', 'desc')
                            ->get();
                }
                $total_row = $data->count();
                if($total_row > 0)
                {
                    foreach($data as $row)
                    {
                        $output .= '
                        <tr>
                            <td>'.$row->Désignation.'</td>
                            <td>'.$row->ID-Pièce.'</td>
                            <td>'.$row->Status.'</td>
                        </tr>
                        ';
                    }
                }
                else
                {
                    $output = '
                    <tr>
                        <td align="center" colspan="5">No Data Found</td>
                    </tr>
                    ';
                }
                $data = array(
                    'table_data'        =>  $output,
                    'total_data'        =>  $total_data
                );

                echo json_encode($data);
            }
        }

and this is my web.php:

Route::get('/recherche', 'PDRController@index');
Route::get('/recherche/action', 'PDRController@action')->name('recherche.action');
Sami
  • 165
  • 4
  • 19
  • Do you get any values on server-side? – Lajos Arpad Jan 07 '19 at 12:06
  • Ofc i have 1 insert in my DB table. – Sami Jan 07 '19 at 12:07
  • https://stackoverflow.com/a/52362153/4593376 – piscator Jan 07 '19 at 12:13
  • your vague description of the problem suggests you haven't done enough (or any) debugging - do you understand how to debug your application and get detailed information about what is happening and whereabouts in the code the problem is likely to be located? Debugging in detail should be your first step when encountering this kind of problem. If you'd done any, you should be able to give us a much more detailed description of your issue (e.g. something like "my ajax call fails when I send data _x_, the error message is _y_ and the status code is _z_". That's the kind of info we expect to get.) – ADyson Jan 07 '19 at 13:25
  • The problem is in recherche/action it doesn't redircet at all it doesn't call the function action in the controller. – Sami Jan 07 '19 at 17:36
  • That's still a (very short) description of what isn't happening. Doing some debugging should start to tell you what _is_ happening instead...again, do you understand how to debug? If not, now would be a great time to learn. – ADyson Jan 07 '19 at 18:41
  • I need to solve this issue asap. But if dubugging is the key can you help me to do so ? – Sami Jan 07 '19 at 19:11
  • You don't need me to repeat what is already well documented elsewhere: https://www.google.com/search?q=how+to+debug+php&rlz=1C1GCEU_enGB821GB821&oq=how+to+debug+php&aqs=chrome..69i57j69i59j69i65l3j69i60.3496j1j7&sourceid=chrome&ie=UTF-8 – ADyson Jan 08 '19 at 10:54

0 Answers0