Deleting Items from Table - Laravel - php

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.

Related

Call a Modal form view in codeigniter

I have a View called Items and inside it i want to use this button to show a Modal form in another View.
<a href="<?php echo base_url('items/Show/'.$value['id']); ?>" type="button" class="btn btn-secondary btn-sm edit btn-orange" data-bs-toggle="modal" data-bs-target=".bs-example-modal-center" title="View">
<i class="fas fa-eye "></i>
</a>
this is the function in Items controller than will load data to the form
function Show($id){
if (isset($id)){
$this->load->model('Itemo');
$data['view']=$this->Itemo->Edit_items($id);
$this->load->view('user/Dashboard/header');
$this->load->view('user/Dashboard/View',$data);
$this->load->view('user/Dashboard/footer');
}else{
redirect('/items/List_cat', 'refresh');
}
}
and this is the View which contains the modal form Called View
<div class="modal fade bs-example-modal-center" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Center modal</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="Close"></button>
</div>
<div class="modal-body">
<!-- /.data loaded here-->
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
</div>
But when i click on the button nothing shows
I think to fix this issue, you can update the data-bs-target attribute in the button tag to match the ID of the modal form's outermost div element, which is "modal-center". You can add this ID to the modal form's outermost div element,
like this:
<div class="modal fade" id="modal-center" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<!-- modal content here -->
</div>
and change button
<a href="<?php echo base_url('items/Show/'.$value['id']); ?>" type="button" class="btn btn-secondary btn-sm edit btn-orange" data-bs-toggle="modal" data-bs-target="#modal-center" title="View">
<i class="fas fa-eye "></i>
</a>

Bootstrap modal doesn't get user ID

I have a problem with a bootstrap modal. I'm using PHP.
In an admin panel there is a table with the list of the users and the possibility to edit or delete a user's profile. For the delete I want to create a modal for the confirm of the delete but, when I click on "confirm" button inside the modal, the modal gets by default the user ID of the first user in the table and not the user ID of the selected user.
Here is the code:
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php var_dump($utente['idUser']); ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
If I make a var_dump of $utente['idUser'] before the modal, it gets the right user ID. If I make it inside the modal it gets by default the first ID, as I said.
Notice that every modal trigger button has a data-target attribute to define which modal will be opened.
In your case, the button of every row you used to triggered the modal have the same data-target, which is #confirmDelete. These modals behind also has the same id called #confirmDelete, so every time you hit the modal trigger button (all had the same data-target) then eventually it will shows up the very first modal element.
For a better understanding, compare my code to yours and see the differences.
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php echo $utente['idUser']; ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
In the above code, I gave every pair of modal elements (modal trigger button and modal ID) a unique data-target value and a unique element id.
...
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>">
...
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
...
Now each pair of modal elements have their own ids and they should be working the way you wanted.

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

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

Pass a value in modal to delete record

I want to make a modal for delete confirmation, but I can't take the value of the record from the table and pass it to the modal and delete the record.
$query1=mysqli_query($conn,"select * from tab1 where username = '$row[username]'");
while($row =mysqli_fetch_array($query1))
{
echo '
<tr class ="item" id=echo $row["country"]">
<td>'.$row["country"].'</td>
<td>
<a class="Danger danger-color" href="#" data-toggle="modal" data-target="#DangerModalalert">
<i class="glyphicon glyphicon-trash"></i></a>
</td>
<div id="DangerModalalert" class="modal modal-edu-general FullColor-popup-DangerModal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-close-area modal-close-df">
<a class="close" data-dismiss="modal" href="#"><i class="fa fa-close"></i></a>
</div>
<div class="modal-body">
<span class="glyphicon glyphicon-trash fa-2x"></span>
<h2>Delete Confirmation</h2>
<p>Are you sure you want to delete it?</p>
</div>
<div class="modal-footer danger-md">
<a data-dismiss="modal" href="#" style="background:red">No</a>
Yes
</div>
</div>
</div>
</div>
First, you only need one modal div. Add class delete-yes to delete button so we can replace it's href later from javascript
<div id="DangerModalalert" class="modal modal-edu-general FullColor-popup-DangerModal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-close-area modal-close-df">
<a class="close" data-dismiss="modal" href="#"><i class="fa fa-close"></i></a>
</div>
<div class="modal-body">
<span class="glyphicon glyphicon-trash fa-2x"></span>
<h2>Delete Confirmation</h2>
<p>Are you sure you want to delete it?</p>
</div>
<div class="modal-footer danger-md">
<a data-dismiss="modal" href="#" style="background:red">No</a>
<a class="delete-yes" href="#" style="background:red">Yes</a>
</div>
</div>
</div>
</div>
Set url to delete link, and class delete-confirm for JS binding:
<a class="Danger danger-color delete-confirm" href="delete.php?id=<?php echo $row["id"];?>" data-toggle="modal" data-target="#DangerModalalert">
<i class="glyphicon glyphicon-trash"></i>
</a>
then add this JS code to bind delete button event
$(document).on("click", ".delete-confirm", function () {
var deleteUrl = $(this).attr('href');
$(".modal-body .delete-yes").attr('href', deleteUrl);
return false;
});

Laravel scheduling app

I am trying to make a front end time table where you can click on a cell for example in row 8:30, and then a modal pops up and you can pick until when you will reserve the term going from 9:00 onward in half hour increments.
Reserving only one term works fine, as I can pick the variable directly from a loop, however I am in doubt as how to forward it to modal.
Controller:
public function index()
{
$scheduler_start_time = Carbon::createFromTime(6, 0, 0);
$scheduler_end_time = Carbon::createFromTime(20, 0, 0);
$equipment = Equipment::with('reservations', 'reservations.user')->get();
return view('landing', compact('equipment', 'scheduler_start_time', 'scheduler_end_time'));
}
Table loop in front-end:
#while($scheduler_start_time < $scheduler_end_time)
<tr>
<td>{{$scheduler_start_time->format('H:i:s')}}</td>
#foreach($equipment as $instrument)
<td>
<a href="#" style="display: block;"
data-href="{{route('reserve-me', [$instrument->id, $scheduler_start_time->timestamp])}}"
data-toggle="modal" data-target="#confirm-reservation">
#if(!$instrument->reservations->where('reserved_from', $scheduler_start_time)->isEmpty())
{{$instrument->reservations->where('reserved_from', $scheduler_start_time)->first()->user->name}}
#else
#endif
</a>
</td>
#endforeach
<?php $scheduler_start_time->addMinutes(30) ?>
</tr>
#endwhile
Modal:
<!-- Modal -->
<div class="modal fade" id="confirm-reservation" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
New reservation
</div>
<div class="modal-body">
Are you sure you want to add this reservation?
<br><br>
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Reserve until
<span class="caret"></span></button>
<ul class="dropdown-menu">
<!-- *CLARIFICATION -->
<li>{{$scheduler_start_time}}</li>
</ul>
</div>
</div>
<div class="modal-footer">
<a class="btn btn-primary btn-ok">Yes</a>
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
</div>
</div>
</div>
</div>
<script>
$('#confirm-reservation').on('show.bs.modal', function(e) {
$(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
});
</script>
Where it says *Clarification: I was trying in small steps and getting at least the value on which I clicked, but I am always getting the final time 20:00h

Categories