I am trying to pass dynamic data to a bootstrap modal. What i want to do is pass the data-id by using jQuery post request. I have my view users containing a list with the users. when i click on details button i want to pass the id using jquery post and then using eloquent, retrieve everything about each user.
my view
<table class="table">
<thead class="thead-default">
<tr>
<th>No.</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php use App\User;
$users = \App\User::where('admin',0)->get();
$i = 1;?>
@foreach($users as $user)
<tr id="{{$user->id}}">
<th scope="row">{{$i}}</th>
<td width="65%">{{ucfirst($user->name)." ".ucfirst($user->surname)}}</td>
<td>
<button class="btn btn-info infoU"
data-toggle="modal"
data-target="#viewUser"
data-id="{{$user->id}}">
<span class="glyphicon glyphicon-file"></span>
Details</button>
</td>
</tr>
<?php $i++ ?>
@endforeach
</tbody>
</table>
my script
$(document).ready(function () {
$(".infoU").click(function (e) {
$('#pic').attr('src',$(this).attr("data-pic"));
$currID = $(this).attr("data-id");
$.post("detail.blade.php", {id: $currID}, function (data) {
$('#user-data').html(data);
}
);
});
});
my modal
<div class="modal fade" id="viewUser" role="dialog">
<div class="modal-dialog" style="min-height: 800px">
<div class="modal-content">
<form method="get" action="">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h2 class="modal-title" style="color: #BC0E91">User Details</h2>
</div>
<div class="modal-body">
<div id="user-data">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
detail.blade.php
<?php
if ($_REQUEST['id']){
$id = $_REQUEST['id'];
echo $id;
}
$usr = \App\User::where('id', $id)->first();
?>
I don't know why this is not working and i am not sure if i can pass a route instead of detail.blade.php in the script. Any help is much appreciated. Thank you