Diao Vũ
Diao Vũ

Reputation: 109

Show database on bootstrap modal with Ajax in Laravel

I'm new at Laravel and ajax, I want to show database from mysql to Bootstrap modal, follow this

I use Laravel framework, this is the table

<tbody>
        <?php
            $result = DB::select('SELECT * FROM thongtinlodat');

            foreach ($result as $key) {
                echo '<tr>';
                    echo '<td>'.$key->TenNhaDauTu.'</td>';
                    echo '<td>'.$key->SoNhaDauTu.'</td>';
                    echo '<td>'.$key->NgayCapNDT.'</td>';
                    echo '<td>'.$key->NoiCapNDT.'</td>';
                    echo '<td>
                            <a class="btn btn-small btn-primary"
                               data-toggle="modal"
                               data-target="#exampleModal"
                               id="getUser"
                               data-whatever="'.$key->ID.' ">VIEW</a>
                         </td>';
                echo '</tr>';
            }
        ?>
</tbody>

Modal

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="memberModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="memberModalLabel">View info</h4>
            </div>
            <div class="dash">
             <!-- Content goes in here -->
            </div>
        </div>
    </div>
</div>

This is javascript with ajax

$('#exampleModal').on('show.bs.modal', function (event) {
          var button = $(event.relatedTarget); // Button that triggered the modal
          var recipient = button.data('whatever'); // Extract info from data-* attributes
          var modal = $(this);
          var id = recipient;
          var dataString = 'id=' + recipient;

            $.ajax({
                type: "GET",
                url: "editdata",
                data: dataString,
                cache: true,
                success: function (data) {
                    console.log(data);
                    modal.find('.dash').html(data);
                },
                error: function(err) {
                    console.log(err);
                }
            });  
    });

File editdata.php, get id from ajax and select data from mysql

<?php
    $id = $_GET['id'];
    $members = DB::table('thongtinlodat')
                ->where('ID', $id)
                ->first();
?>

In the routes, how can I get id to insert to the URL, like this Route::get('/editdata{?id=}', 'adminController@test');? Thanks for helping!!!

Upvotes: 1

Views: 2217

Answers (1)

Leo Thibaudat
Leo Thibaudat

Reputation: 51

In the routes, how can I get id to insert to the URL?

Like this:

Route::get('my/route/{arg1}', 'MyController@show')->where('arg1', '[0-9]+');

And in your controller:

public function show(arg1){...}

The "where" clause allows you to tell the route what to expect as an argument. For example in the lone above, the arg1 must be positive integer.

Looking at your code, I can only recommend you to look at laravel doc :). It's really well written and you will see how to improve your code. More info: https://laravel.com/docs/5.6/routing#route-parameters

Upvotes: 1

Related Questions