在Laravel中使用Ajax显示有关引导模态的数据库

时间:2018-07-26 07:53:37

标签: 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();
?>

在路线中,如何获取ID插入Route::get('/editdata{?id=}', 'adminController@test');这样的URL? 感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

  

在路线中,如何获取ID插入网址?

赞!

using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;

namespace GetPullRequest
{
    class Program
    {
        static void Main(string[] args)
        {

            String collectionUri = "https://xxx.visualstudio.com";
            VssBasicCredential creds = new VssBasicCredential("", "6ztnrtjdd3i42juchu4xxxxxxxxxaslnseo277tgiuiq");
            VssConnection connection = new VssConnection(new Uri(collectionUri), creds);
            var git = connection.GetClient<GitHttpClient>();
            var prId = 12345;
            var pr = git.GetPullRequestByIdAsync(prId).Result;
            var RepoUrl = pr.Repository.RemoteUrl;
            var prUrl = RepoUrl + "/pullrequest/" + prId;
            Console.WriteLine(prUrl);

        }
    }
}

在您的控制器中:

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

“ where”子句允许您告诉路由期望作为参数的内容。例如,在上面的孤行中,arg1必须为正整数。

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