I want to pass the fetched database value to modal popup.I have fetched values from database and listed as table with pagination.
'name' field has link and will show model popup while triggering a link.I want to list the details of name which I check from the link in laravel 5.3.
Now model pop up trigered with 'Modal Header 3' for all link.I want to know how to pass the value modal popup and show the details of particular id. I want to know to know shall I need separate page or controller to do this.Where to write the query for modal popup.How can I display the details of specific Id.
id name age
1 xx 26
2 yy 28
3 zz 30
<table border = 1>
<tr>
<td>ID</td>
<td>Passanger Name</td>
<td>Destination</td>
<td>Created Date</td>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->p_id }}</td>
<td><a href="#" class="viewPopLink" role="button" data-id="{{ $user->p_id }}" data-toggle="modal" data-target="#myModal">{{ $user->p_name }}<a></td>
<td>{{ $user->destination }}</td>
<td>{{ $user->created_date }}</td>
</tr>
#endforeach
</table>
{{$users->links()}}
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header {{ $user->p_id }}</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
You can do it the way #Komal suggested, but I would not do another AJAX request since you already have all the data you need for the modal in your current view. This saves you also from writing another controller.
Add the data you need into the <a> element and use it to fill in the data into the modal popup by adding a simple 'on load' method for the modal.
Here is the code.
<td>
<a href="#" class="viewPopLink" role="button"
data-id="{{ $user->p_id }}" data-name="{{ $user->p_name }}" data-age="{{ $user->p_age }}"
data-toggle="modal" data-target="#myModal">{{ $user->p_name }}<a>
</td>
Add this into the modal body:
<p>
<span>ID</span><span id="modal_p_id"></span>
<span>Name</span><span id="modal_p_name></span>
<span>Age</span><span id="modal_p_age"></span>
</p>
Add this script into your view:
<script>
/* populate the modal popup when it's launched, with the data provided by the launching button .... */
$('#myModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget); // Button that triggered the modal
var id = button.data('id'); // Extract info from data-* attributes
var age = button.data('age');
var name = button.data('name');
// Update the modal's content
var modal = $(this);
modal.find('.modal-title').text('Information for Passenger ' + name);
modal.find('#modal_p_id').text(id);
modal.find('#modal_p_name').text(filename);
modal.find('#modal_p_age').text(age);
});
</script>
Use jquery ajax request and get user data to set modal
$(document).on('click', '.viewPopLink', function() {
var user_id = $(this).data('id');
$.ajax({
url: 'user/get-details',
type: 'GET',
data: 'id='+user_id,
dataType: 'JSON',
success: function(data, textStatus, jqXHR){
var name = data.name;
$('.modal-title').html('<span>Modal Header ' + name + '</span>');
$('#myModal').modal('show');
},
error: function(jqXHR, textStatus, errorThrown){
},
});
});
Controller method
//Get user data
public function getDetails(Request $request)
{
$request_data = $request->all();
$user_id = $request_data['id'];
$user_data = User::where('id', $user_id)->first();
return response()->json($user_data);
}
Related
I want to delete a specific image when user selected the red delete icon which is in the right side of each image .
screenshot-
ss after user click on deleted icon this shows up-
My php codes from where i took the img_id to delete and displaying all images-
<?php
include_once '../../php/connection.php';
$query = "SELECT * FROM img_info LEFT JOIN estate_infos ON img_info.estate_infos_id = estate_infos.id where img_info.estate_infos_id = $mdoalid";
$stmt=$dbcon->prepare($query);
$stmt->execute();
$count = $stmt->rowCount();
$datas=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($datas as $key => $data)
{ $pic = $data['image'];
$a=$data['img_id'];
?>
<img src="../<?php echo $pic ?>" width="360" height="150">
<a data-toggle="modal" data-target="#delimg<?php echo $a; ?>">
<i class="far fa-times-circle fa-2x" aria-hidden="true" style="color:red"></i></a>
<?php echo $a?> // displays the id of each image displayed (wont display in production)
My modal code -
<!-- Modal -->
<div class="modal fade" id="delimg<?php echo $data['img_id']; ?>" tabindex="-1" aria-labelledby="delimglabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="delimglabel">Delete Image</h5>
</div>
<div class="modal-body">
Are you sure you want to delete the image?
<?php echo $data['img_id'] ?>
</div>
<div class="modal-footer">
<button type="button" class="delbtn btn btn-sm btn-secondary text-white btn-danger">Delete</button>
//Here "delbtn" will trigger the ajax delete image from database
</div>
</div>
</div>
</div>
<?php } ?>
My ajax code which is i need to fix -
<script type="text/javascript">
$(document).ready(function() {
$(".delbtn").on("click", function(e) {
e.preventDefault();
alert("Working");
jQuery.ajax({
type: "POST",
url: "image-del.php",
data: < ? php echo $data['img_id'] ? > ,
success: function(response) {
alert('Image Deleted !');
},
error: function(response) {
alert('Image NOT Deleted !')
}
});
});
});
</script>
My mysql pdo code to delete image by id from database , this file name is "image-del.php"-
<?php
include_once 'connection.php';
if (isset($_POST['delbtn'])) {
$img_id = $_POST['img_id'];
$sql = "DELETE FROM img_info WHERE img_id=:img_id";
$stmt = $dbcon->prepare($sql);
$stmt->bindparam(':img_id', $img_id);
$stmt->execute();
?>
So, how can make the specific image get deleted by ajax properly?
-Thank you in advance
This is actually a two step process. Here's the idea:
First, as usual, render all the images along with their button to open the modal (the delete button to open the modal).
<?php foreach ($datas as $key => $data): ?>
<img src="../<?php echo $data['image'] ?>" width="360" height="150">
<a data-toggle="modal" data-target="#delete-modal" data-imgid="<?php echo $data['img_id']; ?>">
<i class="far fa-times-circle fa-2x" aria-hidden="true" style="color:red"></i>
</a>
<?php endforeach; ?>
You do not need to serve the HTML modal inside the loop. You only need one modal. You don't need each modal for each image.
So just change it to this and put it in the bottom of the page:
<div class="modal fade" id="delete-modal" tabindex="-1" aria-labelledby="delimglabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="delimglabel">Delete Image</h5>
</div>
<div class="modal-body">Are you sure you want to delete the image?</div>
<div class="modal-footer">
<button type="button" class="delbtn btn btn-sm btn-secondary text-white btn-danger" data-delete-imgid="">Delete</button>
</div>
</div>
</div>
</div>
After that, you need to trigger an event when the modal opens, get the image id and put it in the data attribute, so that it is used and can be accessed when you make the final request to delete it and send it to the server:
$('#delete-modal').on('show.bs.modal', function(e) { // when the delete modal opens
var imageId = $(e.relatedTarget).data('imgid'); // get the image id
$(e.currentTarget).find('.delbtn').attr('data-delete-imgid', imageId); // and put it in the delete button that calls the AJAX
});
Then like I said above the comments, don't echo the PHP image id in there. Use the ID that you applied in the button:
$(".delbtn").on("click", function(e) {
e.preventDefault();
jQuery.ajax({
type: "POST",
url: "image-del.php",
data: { delbtn: $(this).attr('data-delete-imgid') }
success: function(response) {
alert('Image Deleted !');
},
error: function(response) {
alert('Image NOT Deleted !')
}
});
});
This should serve as a general idea on how to pass the ID from the delete button to the modal, then finally to the server.
I have a generic modal just before the closing body tag of my page. I use Javascript to show the modal when a button is clicked, passing data attribute values embedded within the button to the modal title, body and footer. This makes the modal dynamic. It works great, however, when I add the trigger button within jQuery DataTable, it fails to trigger the modal.
This is my modal:
<div class="modal fade" id="modal_confirm_action" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
<div class="pull-right m-l-15">
<button class="btn btn-danger btn-sm modal_close_btn" data-dismiss="modal" class="close" title="Close"> ×</button>
</div>
</div><!--/.modal-header-->
<div class="modal-body">
<!--render data via JS-->
</div>
<div class="modal-footer">
<a class="btn btn-sm btn-danger" role="button" id="action_url"> Yes, Continue </a>
<button data-dismiss="modal" class="btn btn-sm btn-secondary"> No, Cancel </button>
</div>
</div>
</div>
</div>
This is the button that triggers it:
<a class="btn btn-sm btn-danger text-white modal_trigger_confirm_action" data-title="Delete Post" data-msg="Sure to delete?" data-url="'.base_url('posts/delete_post/'.$id).'">Delete</a>
Note: I'm using server side processing with DataTables (and CodeIgniter), so the button above is inside my controller method, and rendered in one column, like this:
...
$row[] = '<a class="btn btn-sm btn-danger text-white modal_trigger_confirm_action" data-title="Delete Post" data-msg="Sure to delete?" data-url="'.base_url('posts/delete_post/'.$post_id).'">Delete</a>';
...
This is the JavaScript that opens the modal:
$('.modal_trigger_confirm_action').click(function() {
//get data value params
var title = $(this).data('title');
var msg = $(this).data('msg');
var url = $(this).data('url');
$('#modal_confirm_action .modal-title').text(title); //dynamic title
$('#modal_confirm_action .modal-body').html(msg); //dynamic body content
$('#modal_confirm_action .modal-footer #action_url').attr('href', url); //url to delete item
$('#modal_confirm_action').modal('show'); //show the modal
});
Clicking the Delete button within each row doesn't do anything. What am I doing wrong?
Because the button is generated dynamically and not the part of DOM when it loaded first time so you need to trigger click on that dynamically generated button something like this.
$(document).on( "click",".modal_trigger_confirm_action", function() { //logic here });
Please try this:
$( ".modal_trigger_confirm_action" ).on( "click", function() {
// your logic here
});
I/m trying to load dynamic images on a bootstrap modal with ajax when a user clicks on different links on a page. Each link has an data-id that is used to show its relevant image in the modal body. It works fine for the first couple of links but starts to misbehave after 4-5 clicks. Later it starts showing previously loaded images when a link is clicked and the relevant image is shown after several seconds of the modal being triggered. Can anyone help me what I'm doing wrong with my code below:
My JS Code:
$(document).ready(function(){
$(document).on('click', '.viewPhoto', function(e){
e.preventDefault();
var pid = $(this).data('id'); // it will get id of clicked row
$("#photoContent").html("Please Wait...");
$.ajax({
url: "URL OF PAGE",
type: 'POST',
data: 'pid='+pid,
})
.done(function(data){
$('#photoContent').html(data); // load response
})
.fail(function(){
$('#photoContent ').html('Error');
});
});
});
And my modal HTML is:
<div id="viewPhotoModal" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content" >
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×
</button>
<h4 class="modal-title"></h4>
</div>
<div class="modal-body" id="photoContent"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
And the HTML of Link is:
View Image
you should use cache: false in your ajax command.
Here is my blade view, it lists debiturs and all their properties like id, name, address. This also have a modal view button that pops a modal that is supposed to query the debitur by id then show the details of it data-target="#viewModal" onclick='showDetail({{$var->id}});
(ajax_table.blade):
<?php
if(!empty($data)){$isi = json_decode($data);}
if(!empty($modaldata)){$isimodal = json_decode($modaldata);}
?>
#if(!empty($isi->data))
#foreach($isi->data as $var)
#include('userdebitur.ajax_modal_view')
<tr>
<td>{{ $var->id }}</td>
<td>{{ $var->name }}</td>
<td>{{ $var->address }}</td>
#endif
<td>
<div class="btn-group">
<button title="Detail Info" type="button" class="btn btn-default btn-flat" data-toggle="modal" data-target="#viewModal" onclick='showDetail({{$var->id}});'><i class="fa fa-eye"></i></button>
</div>
</td>
</tr>
#endforeach
#else
<tr><td align="center" colspan="5">Not found.</td></tr>
#endif
Here is my modal file that is called from above (ajax_modal_view.blade):
<div class="modal fade" id="viewModal">
<div class="modal-dialog">
<div class="modal-content mdl-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title">Default Modal</h4>
</div>
<div class="modal-body">
<p>test #if(isset($isimodal)){var_dump($isimodal)} #endif</p> //I'm trying to output this
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
The modal view button also called showDetail() passing debitur id as variable then returning data from the controller by calling a function inside ajax.blade:
function showDetail(id)
{
$.ajax({
type:"GET",
url: "{{ url('ajax/userdebitur/') }}"+"/"+id,
dataType: "json",
cache: false,
success: function(modaldata){
$("#viewModal").html(modaldata.view); //this is likely the problem
},
error: function (modaldata) {
//
}
});
}
And this is my controller:
public function getdebitur($id)
{
$content = Debitur::find($id);
$modaldata['content'] = $content;
$modaldata['view'] = view('userdebitur.ajax_modal_view')->with('modaldata', json_encode($content))->render();
echo json_encode($modaldata);
}
When using console in web browser, I have successfully retrieved the selected debitur data showing all the objects, however I can't seem to pass anything to the modal.
I have tried passing the data to the ajax_table.blade itself, moving the modal inside ajax_table.blade, passing data without view from the controller, moving json_decode($modaldata) inside the modal itself, all to no avail.
If in the ajax.blade I use $("#viewModal").html(modaldata.view); modal is showing for one second then disappear, but if I use $("#viewModal").modal(modaldata.view); or $("#viewModal").modal(modaldata); it's showing with no $isimodal value.
What I want to do: getting the value passed from the controller and var_dump it in the modal.
I solved the problem. The error was caused by the modal being updated by the ajax inside another modal, instead of replacing the whole modal.
The solution is to remove <div class="modal fade" id="viewModal"> from my modal file, and then it will render properly.
In my code below, i am displaying a list of students in a table. Now when i delete the last student in the table, it rather deletes the first person. Why is this happening? Could it be i am not looping well to get the id's of each students?
When the delete button is clicked, a modal button pops up and i click on yes to do the deletion like below
PS: Laravel Beginner
Controller
public function index()
{
$students= Student::where('user_id', Auth::user()->id)->get();
return view('students.index',compact('students'));
}
View
<tbody>
#foreach($students as $std)
<tr>
<td>{{$std->name}}</td>
<td>{{$std->phone}}</td>
<td>
<a style="color:#000" href="/student/{{$std->id}}/edit" title="edit" ><i style="font-size:16px" class="fa fa-edit"></i></a>
<a style="color:#000" data-toggle="modal" data-target="#myModal" title="delete" ><i style="font-size:16px" class="fa fa-trash"></i></a>
</td>
</tr>
#endforeach
</tbody>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Warning</h4>
</div>
<div class="modal-body">
<p>Do you wish to delete this student ?</p>
</div>
<div class="modal-footer">
#if(!empty($std))
Yes
<a data-dismiss="modal" class=" modal-action modal-close waves-effect waves-light green white-text btn">No</a>
#else
#endif
</div>
</div>
</div>
</div>
</div>
update
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Warning</h4>
</div>
<div class="modal-body">
<p>Do you wish to delete this student ?</p>
</div>
<div class="modal-footer">
#if(!empty($std))
Yes
<a data-dismiss="modal" class=" modal-action modal-close waves-effect waves-light green white-text btn">No</a>
#else
#endif
</div>
</div>
<script>
$('#myModal').on('show.bs.modal', function(e) {
var $modal = $(this);
var userId = e.relatedTarget.dataset.uid;
// You can add the url with user ID to link href attribute
$modal.find('.modal-footer a.red-text').attr('href', '/customer/' + userId + '/delete');
})
</script>
In your case, the best practice is to send user ID to Bootstrap modal when you click on delete button.
First, add a data-attribute on your delete button to pass the user ID
<table>
<tbody>
#foreach($students as $std)
<tr>
<td>{{$std->name}}</td>
<td>{{$std->phone}}</td>
<td>
<a style="color:#000" href="/student/{{$std->id}}/edit" title="edit">
<i style="font-size:16px" class="fa fa-edit"></i>
</a>
<a style="color:#000" data-toggle="modal" data-target="#myModal" data-uid="{{$std->id}}" title="delete">
<i style="font-size:16px" class="fa fa-trash"></i>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
Then you need to add a bit of jQuery to retrieve your uid parameter
$('#myModal').on('show.bs.modal', function(e) {
var $modal = $(this);
var userId = e.relatedTarget.dataset.uid;
// You can add the url with user ID to link href attribute
$modal.find('.modal-footer a.red-text').attr('href', '/student/' + userId + '/delete');
})
So, you're sure the ID used for deletion will be the correct one.
Hope this helps you :)
Update :
I made a simple codepen to illustrate this
Codepen example
You are doing it in a wrong way, you have to assign std->id to each delete anchor and give an id to anchor id="del-anchor" like
<a id='del-anchor' std-id={{$std->id}} style="color:#000" data-toggle="modal" data-target="#myModal" title="delete" ><i style="font-size:16px" class="fa fa-trash"></i></a>
You also need to assign id to modal anchor and change it dynamically.
$(document).on('click', '#del-anchor', function () {
var std-id = $(this).attr('std-id');
var href = "/student/"+std-id+"/delete";
$("your-model-anchor").attr("href",href);
$('#myModal').modal();
}
It will delete the corresponding student, you also need to call $("#myModal").hide when delete successful.