我是 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">×</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/