How do i make a modal accept data when button is clicked? - laravel

I have a bootstrap modal. i call it using the #data-target. Problem i have is I'd like to populate the modal with the details from item on a list of feedbacks which i fetch from the database.
I want it dynamic, so it shows data of any item clicked from the loop. I'm really new to js and dont know ho to handle it. here's my laravel code for the feedbacks:
<div class="body">
#if(count($feedbacks) > 0)
<ul class="basic-list">
#forelse($feedbacks as $feedback)
<li>{{ $feedback->subject }} <span class="pull-right label-info label">View</span></li>
#empty
<p class="text-danger">No Feedbacks Yet</p>
#endforelse
#endif
</ul>
</div>
and this is the modal:
<div class="modal fade" id="defaultModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="defaultModalLabel">{{ $feedback->subject }}</h4>
</div>
<div class="modal-body">{{ $feedback->content }}</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect">SAVE CHANGES</button>
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
</div>
</div>
The modal shows right now, but doesn't pass data when clicked.

if you want to avoid multiple modals, and still get data from your database, perform an ajax call to retrieve the $feedback->content by its id
Remove the data-target and data-toggle attribute from your link
Add a class attribute and a unique id = {{$feedback->id}} to the link
<div class="body">
#if(count($feedbacks) > 0)
<ul class="basic-list">
#forelse($feedbacks as $feedback)
<li>{{ $feedback->subject }} <span class="pull-right label-info label">View</span></li>
#empty
<p class="text-danger">No Feedbacks Yet</p>
#endforelse
</ul
#endif
</div>
create a route that accepts the feedback id as a url parameter
Route::get('/get-feedback-content/{feedback}', 'YourController#getFeedbackContent');
Add this to YourController:
public function getFeedbackContent($feedback){
//this assumes you have a feedback model, change as appropriate
$fb = FeedbackModel::find($feedback);
//return an feedback object as an array
return response()->json(array('feedback'=> $fb), 200);
}
Using jQuery your script should look something like this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".feedback-open").click(function(){
id = $(this).attr('id');
getMessage(id);
})
function getMessage(id){
$.ajax({
type:'GET',
url:'/getmsg/'+id,
data:'_token = <?php echo csrf_token() ?>',
success:function(data){
$("#defaultModal .modal-title").html(data.feedback.subject);
$("#defaultModal .modal-body").html(data.feedback.content);
$('#defaultModal').modal('show');
}
});
}
});
</script>

Modal have to be unique for every data and you didn't use {{}} to echo data, Please have a look the following code:
<div class="body">
#if(count($feedbacks) > 0)
<ul class="basic-list">
#foreach($feedbacks as $key => $feedback)
<li>
{{ $feedback->subject }}
<a href="javascript:void(0);" data-toggle="modal" data-
target="#modal-{{$key}}"><span class="pull-right label-info label">View</span></a>
<div class="modal fade" id="modal-{{$key}}" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="defaultModalLabel">{{$feedback->subject}} </h4>
</div>
<div class="modal-body"> {{$feedback->content}}</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect">SAVE CHANGES</button>
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
</div>
</div>
</li>
#endforeach
</ul>
#else
<p class="text-danger">No Feedbacks Yet</p>
#endif

I think you have not displayed both fields i.e. $feedback->subject and $feedback->content into modal
popup.
Please try below code to display the subject and content fields into model as per [Laravel]:
1) {{ $feedback->subject }}
2) {{ $feedback->content }}
<div class="body">
#if(count($feedbacks) > 0)
<ul class="basic-list">
#forelse($feedbacks as $feedback)
<li>{{ $feedback->subject }} <span class="pull-right label-info label">View</span></li>
<div class="modal fade" id="defaultModal_{{ $feedback->id }}" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="defaultModalLabel">{{ $feedback->subject }}</h4>
</div>
<div class="modal-body">{{ $feedback->content }}</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect">SAVE CHANGES</button>
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
</div>
</div>
#empty
<p class="text-danger">No Feedbacks Yet</p>
#endforelse
#endif
</ul>
</div>
Hope this helps you.

Related

How to keep the modal open if the validation is failed in Laravel 8

I am trying to show the modal with the errors after the failure of the validation directly.
My code now shows the modal with the errors only if I clicked the button that shows it (to add a new user).
I have found some related answers but they did not work for me.
My controller
function add(Request $req){
$validated = $req->validate([
'name' => 'required|regex:/^[\pL\s\-]+$/u',
'email' => 'email|unique:users,email',
'password' => 'required|min:8',
]);
modal.blade
<form action="addUser" method="post" enctype="multipart/form-data">
#csrf
<div class="modal fade text-left" id="ModalCreate" tabindex="-1" role="dialog" aria-hidden="true" >
<div class="modal-dialog modal-lg" role="document" >
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">{{__('Add New User')}} </h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
...
</div>
</div>
</div>
</div>
</form>
The page that calls the modal
#extends('layouts.admin')
#section('content')
<div class="container">
<div class="float-right">
<a href="#" data-toggle="modal" data-target="#ModalCreate">
<button type="button" class="btn btn-dark"><i class="bi bi-person-plus"></i></button>
</a>
</div><br><br>
<table class="table">
...
</table>
</div>
#include('admin.Users.addUser')
#endsection

Laravel vue js modal hide curret modal and show new modal with bootstrap 5

I have 3 components in my vue js
create.vue
list-user.vue
modal-role.vue
in my create.vue i call the 2 other component,
<template>
<div>
<modal-role></modal-role>
<list-user></list-user>
</div>
</template
these 2 components are all modal
modal-role.vue
<template>
<div>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalRole">
add members
</button>
<!-- Modal -->
<div class="modal fade" id="modalRole" tabindex="-1" aria-labelledby="modalRoleLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalRoleLabel">Role</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<ul class="list-group">
<li #click="selectRole(item)" data-bs-toggle="modal" data-bs-target="#modalUser" data-bs-dismiss="modal" class="list-group-item list-group-item-action" v-for="item in roles" :key="item.id"> {{ item.name }} </li>
</ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</div>
</template>
list-user.vue
<div class="modal fade" id="modalUser" tabindex="-1" aria-labelledby="modalUserLabel" aria-hidden="true">
<div class="modal-dialog" v-if="role">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalUserLabel">Choose members as {{ role.name }} </h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" #click="dismiss()"></button>
</div>
<div class="modal-body">
<ul class="list-group">
<li
class="list-group-item list-group-item-action"
v-for="item in users" :key="item.id"
>
{{ item.name + ' ' + item.surname}}
</li>
</ul>
<div class="row justify-content-center" v-if="!isLast">
<button class="btn btn-primary" #click="loadMore()">
View more
</button>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" #click="dismiss()">Cancel</button>
<button type="button" class="btn btn-primary" #click="choose()" data-bs-dismiss="modal">Choose</button>
</div>
</div>
</div>
</div>
So in my create.vue when i have button from my modal-vue to show a modal with list of role,
in my modal-role.vue what i expected is when i select one role, it dismiss the modal on modal-role.vue and show a new modal that is in list-user.role but I got an error:
app.js:14656 Uncaught TypeError: Illegal invocation
at Object.findOne (app.js:14656:44)
at Modal._showElement (app.js:16567:38)
at app.js:16490:35
at execute (app.js:13914:5)
at app.js:16196:7
at execute (app.js:13914:5)
at HTMLDivElement.handler (app.js:13937:5)
and if tried to put the modal in list-user.vue to modal-role.vue, it work but if the second modal dismiss but i still have the overlay background from one of these 2 modals
EDIT
When i show the first modal, got 2 modal backdrop in my element.
I got the problem here, i put a v-if in my modal-dialog div in my list-user.vue, so my modal have no div dialog in mounted then modal bootstrap can't call it to show, so i move my if statement in my modal-content and it work well

Cannot call modal in another page

I make button on index.php file and want call modal in another page, because when modal in same file and i refresh the page modal seen temporarily.
index.php
<a href="modal.php" data-target="#cancel{{ $form->id }}" class='btn btn-danger btn-sm' data-toggle="modal">Cancel</button>
modal.php
#foreach($list_form as $form)
<div class="modal fade" id="cancel{{ $form->id }}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header" style="background-color: red;">
<h3 class="modal-title h3" id="exampleModalSmLabel" style="color: white;"><b>Cancel Description</b>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true"><h1><b>×</b></h1></span>
</button>
</h3>
</div>
<div class="modal-body">
#if(auth()->user()->role == 'Admin')
<form action="{{ route('form001.cancel', $form->id) }}" method='post'>
#csrf
<div class="form-group">
<label for="cancel_deskripsi">Alasan Cancel</label>
<textarea class="form-control" id="cancel_deskripsi" name="cancel_deskripsi" placeholder="tuliskan alasan anda..."></textarea>
</div>
<div class="modal-footer">
<button type='submit' class='btn btn-danger btn-sm'>Submit</button>
</div>
</form>
#endif
{{ $form->cancel_deskripsi }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endforeach
Am I doing writing anything wrong?

How to retrieve the id from iteration to the bootstrap modal?

I need to capture the id from the iteration, but it seems to capture the last id which it's seems to be obviously,
Am using bootstrap modal which i even don't know how they code their jQuerys'.
Thus how can i do to make this button not capturing the last id?
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#applications_{{$user->id}}" id="#applications_{{$user->id}}">View applications</button>
<!-- Modal -->
<div class="modal fade" id="applications_{{$user->id}}" role="dialog" >
<div class="modal-dialog" >
<!-- Modal content-->
<div class="modal-content" style="width: 600px !important;">
<div class="modal-header">
{{-- <button type="button" class="close" data-dismiss="modal">×</button> --}}
<h4 class="modal-title">Applications</h4>
</div>
<div class="modal-body">
<div class="text-muted" style="float: left;padding-right: 10px">
<table class="table table-hover">
<tr>
<th>Company name</th>
<th>Location</th>
<th>Program</th>
<th>Application</th>
<th>Actions</th>
</tr>
#foreach($user->post as $form)
<tr>
<td>{{$form->pivot->company_name}}</td>
<td></td>
<td>{{$form->pivot->id}}</td>
<td>
/* this button is only capturing the last id from iteration */
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#application_{{$form->pivot->id}}" >View {{$form->pivot->file}}</button> </td>
<td></td>
</tr>
#endforeach
{{$user->name}}
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="application_{{$form->pivot->id}}" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Application</h4>
</div>
<div class="modal-body">
<div class="text-muted" style="float: left;padding-right: 10px">
<div class="center image">
<img src="/storage/file/{{$form->pivot->file}}" class="img-round" alt="User Image">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Expected to retrieve an image from it's respectively post, but it only works with the last id, no errors.
Your second modal is outside the foreach, then id="application_{{$form->pivot->id}}" only works with the last value it took in the loop. To do it with a blade, you can redo the loop and create a modal for each id and image:
<!-- 2nd Modal -->
#foreach($user->post as $form)
<div class="modal fade" id="application_{{$form->pivot->id}}" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Application</h4>
</div>
<div class="modal-body">
<div class="text-muted" style="float: left;padding-right: 10px">
<div class="center image">
<img src="/storage/file/{{$form->pivot->file}}" class="img-round" alt="User Image">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endforeach

Laravel 5.3 pass data to Modal to edit the Comment

Here is my button:
my $post->comment contained id, comment, email and name;
I want to pass the comment to the form inside the modal.
I also want to use the id to the 2nd parameter of the route to update the comment
#foreach($post->comments as $comment)
<button class="btn btn-success btn-xs " data-toggle="modal" data-target="#myModal" data-id="{{ $comment->id }}" data-comment=" {{ $comment->comment }}"><i class="fa fa-pencil"></i></button>
#endforeach
Here is my modal:
<!-- Modal -->
<div class="modal fade modal-md" 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">Comment Update!</h4>
</div>
<div class="modal-body">
<div id="comment-form" class="">
{{ Form::open(['route' => ['comments.store', $post->id], 'method' => 'POST']) }}
<div class="row">
<div class="col-md-12">
<b>{{ Form::label('comment', "Comment:") }}</b>
{{ Form::textarea('comment', null, ['class' => 'form-control', 'rows' => '5', 'id'=>'comment ']) }}
</div>
</div>
{{ Form::close() }}
</div>
</div>
<div class="modal-footer">
<a href="{{route('comments.store',$post->id) }}"
onclick="event.preventDefault();
document.getElementById('comment-update').submit();" class="btn btn-primary">
Update </a>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
I tried this Script but it wont work:
<script type="text/javascript" charset="utf-8">
$('#myModal').on('show', function(e) {
var link = e.relatedTarget();
var id = link.data("id");
var comment = link.data("comment");
var modal = $(this);
modal.find("#id").val(id);
modal.find("#comment").val(comment);
});
you must using on click event in javascript
example:
<button class="btn btn-success btn-xs add" data-toggle="modal" data-target="#myModal" data-id="{{ $comment->id }}" data-comment=" {{ $comment->comment }}"><i class="fa fa-pencil"></i></button>
this script.
$(document).on('click', '.add', function() {
$('#id_kategori').val($(this).data('id'));
$('#comment').val($(this).data('comment'));
$('.form-horizontal').show();
$('#myModal').modal('show');
});
i hope my answer helping you

Resources