How to get modal confirm delete showing the data using laravel? - php

i want to get value id from my table or my db , when i tried to delete one of the row (one of the data). I tried to expand my endforeach and it's just make the html become so weird. Um for more explaining here is my images.
Images confirm modal
yeah i just want to confirm the user if it clicked the delete button it will show a modal "are you sure you want to delete this?".So, the problem is, i don't know how to get the value id when i clicked the next delete button from the seccond row delete button, delete button from the third row delete button, delete button from the fourth row delete button (if there is a row bellow it) and so on.
here is my modal code
Modal Delete
<div class="m-2">
<div class="modal fade h-50" id="modalDelete" tabindex="-1" role="dialog" aria-
labelledby="deleteModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModal">Change Department Status</h5>
</div>
<div class="modal-body">
<p id="question">Are You sure want to delete {{$ideaprogram[0]->showidea_id}}?</p>
</div>
<div class="modal-footer">
<a id="deleteData">
<button type="button" class="btn btn-success">Yes</button>
</a>
<button type="button" class="btn btn-danger" data-dismiss="modal">No</button>
</div>
</div>
</div>
</div>
</div>
i'm just make the value of index $ideaprogram[0]->showidea_id like that. Using zero (0) the hardcode. for example if i want to delete the next one (the next index) which is 1, how can i change it the value into 1 or change it the value into 2 if i want to delete the next index and so on. Basically i want the function more like when i clicked the delete button from some row, it will show the id program from that row. I'm using laravel and php at the moment. I don't know how to make it happen. Can someone help me?

Set data in modal on delete button click :
assuming that you are showing your data in table , then give attribute data-programid to delete button.
#foreach($programs as $program)
<tr>
<td>{{$program->name}}</td>
<td><button class="btn btn-danger deleteProgram" data-programid="{{$program->id}}">Delete</button></td>
</tr>
#endforeach
now we set data in modal and show modal, when user click on deleteProgram button class javascript
<script>
$(document).on('click','.deleteProgram',function(){
var programID=$(this).attr('data-programid');
$('#app_id').val(programID);
$('#question').append(programID+' ?');
$('#applicantDeleteModal').modal('show');
});
</script>
your modal:
<div id="applicantDeleteModal" class="modal modal-danger fade" tabindex="-1" role="dialog" aria-labelledby="custom-width-modalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog" style="width:55%;">
<div class="modal-content">
<form action="{{route()}}" method="POST" class="remove-record-model">
{{ method_field('delete') }}
{{ csrf_field() }}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h5 class="modal-title text-center" id="custom-width-modalLabel">Change Department Status</h5>
</div>
<div class="modal-body">
<h4 id="question">Are You sure want to delete </h4>
<input type="hidden" name="applicant_id" id="app_id">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger waves-effect remove-data-from-delete-form">Delete</button>
</div>
</form>
</div>
</div>
</div>

I use like this all time.
<td>
<ul style="list-style-type: none;">
<li><i class="fa fa-edit"></i> Edit </li>
<li><i class="fa fa-trash"></i>
<button data-toggle="modal" data-target="#modal{{ $role->id }}">Delete</button>
</li>
</ul>
</td>
<!-- Modal -->
<div class="modal fade" id="modal{{ $role->id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header bg-danger">
<h5 class="modal-title" id="exampleModalLabel">You are about to delete the role {{ $role->title }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body text-danger">Once you delete, it cannot be undone.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<form method="POST" action="{{ url('') }}/en/admin/role/delete/{{ $role->id }}">
{{ method_field('delete') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger">Confirm</button>
</form>
</div>
</div>
</div>
</div>

Actually i want an answer that give me a solutions more like A.A Noman since it will be good for a little line of code, so i tought it possible, but after trying the suggested solutions, and not give me that i want, so i think i can solve it like this.
script.js
function deleteModal(data){
document.getElementById('question').innerHTML = `Are you sure to delete ${data.showidea_id} ?`;
document.getElementById('deleteData').setAttribute('href',`/program_idea/${data.showidea_id}/deleteData`);
$('#modalDelete').modal('show');
}
and my HTML are like this
index.blade.php
#foreach($ideaprogram as $dataload)
<tr>
<td class="text-center">
<button type="button" class="btn btn-danger btn-sm deleteProgram" data-toggle="modal"
onclick="deleteModal({{$dataload}})">
<i class="far fa-trash-alt"></i>
</button>
</tr>
#endforeach
anyway thank you for everyone that answer my questions

Related

Implement Modal Popup or redirection on single button in Laravel

I have button in my blade View. What i wish is onclick, I want let the verified users to go to dashboard otherwise open a Modal Popup and ask them to register or login first.
I've created a popup modal with dummy data that works fine for guest users but don't know the logic or code for verified users to let them navigate to their dashboard page.
Any suggestion please.
Blade
<main id="main">
#section('content')
<!-- Page Content -->
<section class="py-5 product_1">
<div class="container">
<div class="align-self-center mx-auto text-center py-5">
<span>Generate A Dynamic QR CODE for Sellers</span>
</div>
<!-- Button trigger modal -->
<button type="button" class="btn buy-button" data-toggle="modal" data-target="#exampleModalCenter">
Generate Security
</button>
#guest
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
#endguest
<script>window.location = "/dashboard";</script>
</div>
</section>
#endsection
</main>
You used #guest and #endguest there's also #auth and #endauth for when you have a logged in user.
I figured it out myself.
I created 2 buttons.
I wrapped one button into #guest and the other one into #auth and it worked for me.
#guest
<!-- Button trigger modal -->
<button type="button" class="buy-button" data-toggle="modal" data-target="#myModel">
Generate Security
</button>
#endguest
and
#auth
<button type="button" class="buy-button"
onclick="window.location='{{route('createCode',['code'=> 'dashboard']) }}';" >
Generate Security
</button>
#endauth

How to pass #foreach value into Bootstrap Modal? Laravel 7.3

I have a list of categories, from where I want to pass {{category.name}} to data into Bootstrap modal.
I want to achieve that when I click on the specific button, to show the name of the category in the modal as a message?
Categories
<div class="card mt-2">#foreach($categories as $category)
<ul class="list-group list-group-flush">
<li class="list-group-item">{{ $category->name}}
<a href="" class="btn btn-danger btn-sm ml-2 float-right"
onclick="handleDelete({{$category->id}})">Delete</a>
Edit
</li>
</ul>
#endforeach
</div>
From where I've tried to pass {{$category->name}} into div element, but It showing the last element from the loop.
Modal
<form action="" method="POST" id="deleteCategoryForm">
#csrf
#method('DELETE')
<div class="modal fade" id="deleteModalCenter" tabindex="-1" role="dialog" aria-labelledby="deleteModalCenterTitle"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Delete category</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete "{{$category->name}}" ?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-dismiss="modal">Go Back</button>
<button type="submit" class="btn btn-danger">Yes, Delete</button>
</div>
</div>
</div>
</div>
</form>
JS Script
<script>
function handleDelete(id){
event.preventDefault();
var form = document.getElementById('deleteCategoryForm');
console.log('Delete', form);
form.action = '/categories/' + id;
$('#deleteModalCenter').modal('show');
}
</script>
Thanks in advance

Passing data from a foreach loop to a bootstrap modal in the same view

Inside a view blade.php, I have a problem passing data within a foreach loop to a outside loop bootstrap modal.
show.blade.php:
{!! Form::open(['method' => 'PATCH','action' => ['OrderController#update',$order_items->id], 'class'=>'table_form']) !!}
#foreach($order_items as $oi)
<tr class="order-form-row">
<td>
{{ $oi->name }}
</td>
<td>
<a class="btn btn-block" data-toggle="modal" data-target="#deleteLineItemModal"><i class="icon-trash"></i></a>
</td>
</tr>
#endforeach
{!! Form::close() !!}
{{--bootstrap modal--}}
<div class="modal fade" id="deleteLineItemModal" tabindex="-1" role="dialog" aria-labelledby="deleteLineItemModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body edit-content">
<h5 class="text-center">Are you sure you want to delete this line item? {{$oi->id}}</h5>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger pull-left">Yes, I am sure</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">No, not today</button>
</div>
</div>
</div>
</div>
When user click the delete icon for any record inside the table, it will pop up the modal to ask user if they want to delete selected row of record. How can I solve this problem?
You need to use javascript/jquery to pass values to modal.
{!! Form::open(['method' => 'PATCH','action' => ['OrderController#update',$order_items->id], 'class'=>'table_form']) !!}
#foreach($order_items as $oi)
<tr class="order-form-row">
<td>
{{ $oi->name }}
</td>
<td>
<a data-item="{{ $oi->id }}" class="btn btn-block" data-toggle="modal" data-target="#deleteLineItemModal"><i class="icon-trash"></i></a>
</td>
</tr>
#endforeach
{!! Form::close() !!}
{{--bootstrap modal--}}
<div class="modal fade" id="deleteLineItemModal" tabindex="-1" role="dialog" aria-labelledby="deleteLineItemModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body edit-content">
<h5 class="text-center">Are you sure you want to delete this line item? {{$oi->id}}</h5>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger pull-left">Yes, I am sure</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">No, not today</button>
</div>
</div>
</div>
<script>
$(document).on("click", ".btn-block", function () {
var itemid= $(this).attr('data-item');
$("#lineitem").attr("href","/orders/line-item-delete/"+itemid)
});
</script>

(Laravel) Confirmation modal on delete button

I have a view which display a table of users from the database and the last column has delete button on it which currently work fine for me but i want to display confirmation model and when the user click on Delete button it will delete the selected use
This is the current code for delete button which is work fine:
<button type="button" class="btn mr-0 mb-0 btn-outline-primary btn-sm"><i class="icon-trash3"></i></button>
Now i used this modal which i have to click on delete button in order to delete the selected user
<div class="modal fade text-xs-left" id="iconModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-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" id="myModalLabel2"><i class="icon-warning2"></i> Confirmation Message</h4>
</div>
<div class="modal-body">
<p>Are you sure that you want to <strong>Delete</strong> this user ?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn grey btn-outline-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-outline-primary">Delete</button>
</div>
</div>
</
You do not need to generate a modal for each users. Just build a single modal and use data- attributes for dynamic parts.
So, as minimal, use data- attribute for every button, e.g.
data-user-id="{{ }}" to use in form action:
<i class="icon-trash3"></i>
In the modal window use a form with delete method:
<form id="deleteUserForm" method="POST">
{{ method_field("DELETE") }}
<button class="btn btn-danger" type="submit">DELETE</button>
</form>
In script use:
<script>
$('#deleteUserForm').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
$('#deleteUserForm').attr('action', '/user/' + button.data('user-id'));
});
</script>
In route use:
Route::delete('/user/{user}', 'UserController#destroy');
P.S. You may also use other data attributes ti use dynamically e.g. data-user-name="{{ }}" to show the user's name will be deleted.
Ok, I have used this method below to make it work but that will need to make a modal for each delete button so i don't think this is a good solution for backend may have more than 1k users or even 200k users so i want this to work when the modal outside the loop ?
#foreach ($users as $user)
<tr>
<th scope="row">{{ $loop->index + 1 }}</th>
<td>{{ $user->fullname }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->email }}</td>
<td>
#foreach( $roles as $role ) {{ $user->role_id == $role->id ? $role->name : ''}} #endforeach
</td>
<td>
<a href="{{ route('dUseInfo', [$user->id]) }}">
<button type="button" class="btn mr-0 mb-0 btn-outline-primary btn-sm"><i class="icon-settings2"></i></button>
</a>
<button type="button" class="btn mr-0 mb-0 btn-outline-primary btn-sm" data-toggle="modal" data-target="#iconModal-{{ $user->id }}"><i class="icon-trash3"></i></button>
<!-- Modal -->
<div class="modal fade text-xs-left" id="iconModal-{{ $user->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-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" id="myModalLabel2"><i class="icon-warning2"></i> Confirmation Message</h4>
</div>
<div class="modal-body">
<p>Are you sure that you want to <strong>Delete</strong> this user ?</p>
</div>
<div class="modal-footer">
{!! Form::open(['route' => ['deleteUser', $user->id], 'class' => 'delete', 'method' => 'DELETE']) !!}
<button type="button" class="btn grey btn-outline-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-outline-primary">Delete</button>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</td>
</tr>
#endforeach

Display modal when get parameter is set, Php bootstrap

I am trying to display modal when user click on link and set parameter, but nothing happens and I don't know why this code doesn't work. Somebody to help ? Thanks
<div class="container-fluid" style="margin: 0 auto; width: 50%">
<div class='btn-group'>
<button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false' style='background-color: #4076BC; color: #fff;'>
Action<span class='caret'></span>
</button>
<ul class="dropdown-menu">
<li>Edit</li>
<li>Delete</li>
</ul>
</div>
</div>
<?php
if (isset($_REQUEST["deleteUser"])) {
echo '<div id="exampleModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-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">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>';
}
?>
if you put modal code inside php if condition then it will not be inserted in the DOM. Place the modal code as it is. It will not display. Use Jquery to detect click or you can insert display block class in modal to show it and also use jquery to set form values to delete

Categories