Pass a value in modal to delete record - php

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;
});

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>

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

Why when the "Remove" link to remove the registration type is clicked it appears the modal to remove the conference?

I have a file that has two modals, one to remove a conference other to remove a registration type type.
The issue is that when I click in the "Remove" link to open the modal to remove the registration type that has a div with id "removeRtype" the modal that appears is always the modal to remove the conference that has a div with id "removeConference".
Do you know where is the issue?
The two modals code is below.
Remove Registration type modal:
Link to open registration type modal:
<label class="form-check-label" for="exampleRadios1">
{{$rtype->name}} <a data-toggle="modal" class="btn btn-sm btn-outline-light-gray ml-4"
id="removeRtype"
data-target=".bd-example-modal-lg" data-modal="removeRtype" href=""><i class="fa fa-times" aria-hidden="true"></i> Remove</a>
</label>
Modal to remove registration type:
<div class="modal fade bd-example-modal-lg" id="removeRtype" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel" data-modal="removeRtype">Remove registration type</h5>
<button type="button" class="close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row d-flex justify-content-center">
<p>Remove registtration type?</p>
<button class="btn btn-outline-primary" id="cancel_remove" href="#" data-dismiss="modal">No</button>
<a class="btn btn-primary ml-2" id="confirm_remove"
href="{{route('rtype.remove', ['id' => $conference->id])}}">No</a>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="close_login_modal" class="btn btn-primary"
data-dismiss="modal">Close
</button>
</div>
</div>
</div>
</div>
Link to open the modal to remove the conference:
#if($conference->registrations->count() == 0)
<a data-toggle="modal" class="btn btn-outline-light-gray ml-2" id="removeConference"
data-target=".bd-example-modal-lg" href="">Remover</a>
#endif
Modal to remove the conference:
<div class="modal fade bd-example-modal-lg" id="removeConference" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel" data-modal="removeConference">Remove conference</h5>
<button type="button" class="close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row d-flex justify-content-center">
<p>Remove conference?</p>
<button class="btn btn-outline-primary" id="cancel_remove" href="#" data-dismiss="modal">No</button>
<a class="btn btn-primary ml-2" id="confirm_remove"
href="{{route('conference.remove', ['id' => $conference->id])}}">No</a>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="close_login_modal" class="btn btn-primary"
data-dismiss="modal">Close
</button>
</div>
</div>
</div>
</div>
Try it like this :
The first :
<label class="form-check-label" for="exampleRadios1">
{{$rtype->name}}
<a data-toggle="modal" class="btn btn-sm btn-outline-light-gray ml-4" href="#removeRtype">
<i class="fa fa-times" aria-hidden="true"></i> Remove
</a>
</label>
The second :
#if($conference->registrations->count() == 0)
<a data-toggle="modal" class="btn btn-outline-light-gray ml-2"
href="#removeConference">Remover</a>
#endif
One more thing i see that you have the modal and the link with the same id, that's a problem :
The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).

Deleting Items from Table - Laravel

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.

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