javascript - 在 Laravel 中使用 Ajax 在引导模式上显示数据库

标签 javascript php ajax laravel

我是 Laravel 和 ajax 的新手,我想显示从 mysql 到 Bootstrap 模式的数据库,关注 this

我用的是Laravel框架,这是表格

<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 -->
<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>

这是带有 ajax 的 javascript

$('#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);
                }
            });  
    });

文件editdata.php,从ajax获取id,从mysql中选择数据

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

在路由中,如何获取要插入到 URL 中的 ID,例如 Route::get('/editdata{?id=}', 'adminController@test');? 谢谢帮忙!!!

最佳答案

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

像这样:

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

在你的 Controller 中:

public function show(arg1){...}

“where”子句允许您告诉路由期望什么作为参数。比如上面那个arg1必须是正整数。

看你的代码,我只能推荐你看 laravel doc :)。它写得非常好,您将看到如何改进您的代码。 更多信息:https://laravel.com/docs/5.6/routing#route-parameters

关于javascript - 在 Laravel 中使用 Ajax 在引导模式上显示数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51533697/

相关文章:

javascript - 如何计算所有可能的值组合以涵盖动态金额?

javascript - 如何通过单击按钮获取单元格值

php - 通过 'forcing' 重定向到父窗口来避免 iframe?

php - 在 PHP 中,是否有一种万无一失的方法来确保远程文件是 CSS 文件?

jquery - 在 Node js Rest Web api 中无法获取请求正文

javascript - 使用 jQuery/ajax 的基本身份验证

javascript - 调用回调方法

javascript - 将由 SQL 构建的 JSON 对象从 PHP 传递到 JavaScript

javascript - 如何从模块发送响应,向其传递 RESTIFY 中的请求和响应对象

PHP odbc_result() 似乎在 5.5+ 中为 varchar 字段返回随机未初始化的内存;在 5.4 中工作的相同代码