Cakephp 3 delete entry confirmation using bootstrap modal? - php

I am trying to use a bootstrap modal to confirm delete operation. I am using Cakephp 3 and am new to it.
So far I am able to create this much.
If i can successfully replace with the id of the button that was clicked, Then the issue will be solved
UsersController.php
public function delete($id)
{
$this->request->allowMethod(['post', 'delete']);
$user= $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success(__('The user with id: {0} has been deleted.', h($id)));
return $this->redirect(['action' => 'index']);
}
}
index.ctp
<?= $this->Html->tag('i','',['class' => 'fa fa-times fa-fw icon-delete deleteUser', 'data-toggle' => 'modal', 'data-target' => '#confirmModal' , 'id' => $user->user_id ]) ?>
<!-- Modal -->
<div class="modal fade" id="confirmModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">Are you sure?</div>
<div class="modal-footer">
<?= $this->Form->postLink(
$this->Html->tag('button','Delete',['class' => 'btn btn-default pull-right']),
['action' => 'delete', <id_here>],
['escape' => false])
?>
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
The generated markup for the delete button part in the modal is
<form action="/boot/users/delete/ABCD20090004" name="post_56e141b0297cc915184088" style="display:none;" method="post"><input name="_method" value="POST"></form>
<a href="#" onclick="document.post_56e141b0297cc915184088.submit(); event.returnValue = false; return false;">
<button class="btn btn-default pull-right">Delete</button>
</a>

Try:
<?= $this->Form->postLink(
$this->Html->tag('button','Delete',['class' => 'btn btn-default pull-right']),
['action' => 'delete',array('id'=>$user->user_id)],
['escape' => false])
?>
Note: you will need a modal for each element in your list(loop)

Pass the user id of the record to be deleted on the delete button on the modal as opposed to the delete button you are clicking on to display the modal. Modify the index.ctp as follows
Do not pass the user id at this point
<?= $this->Html->tag('i','',['class' => 'fa fa-times fa-fw icon-delete deleteUser', 'data-toggle' => 'modal', 'data-target' => '#confirmModal']) ?>
Pass the id on the delete button below
<!-- Modal -->
<div class="modal fade" id="confirmModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">Are you sure?</div>
<div class="modal-footer">
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['class' => 'btn btn-default pull-right']); ?>
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
Also, do not forget to initialize the modal
<script>
$(document).ready(function () {
$('#confirmModal').modal('show');
});
</script>

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 use data-id in form action of Bootstrap Modal

I want to create modal for update one of the column value from bootstrap table. here is the html code for opening modal.
<a href='' id='upload_link' data-id='$row->id' data-toggle='modal' data-target='#myModal'>Upload</a>
This is My Modal Code. In modal I have form and i want to pass data-id value in form action with route {!! Form::open(['route' => ['path_here',data-id] , 'files' => 'true','method' => 'get']) !!} so that i can update value of that particular record. How to pass data-id value in form action. How to do so ! please Help..
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Title</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
{!! Form::open(['route' => ['path_here',data-id] , 'files' => 'true','method' => 'get']) !!}
{!! Form::close() !!}
</div>
<!-- <div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div> -->
</div>
</div>
</div>

How to fix _token error on 'Illuminate \ Database \ Eloquent \ MassAssignmentException'

I have facing this error when try to add new categories to store the data, but it seen show me _token error?
CategoriesController.php
public function store(Request $request)
{
Category::create($request->all());
return back();
}
index.blade.php
<a class="btn btn-primary pull-right navbar-right" data-toggle="modal" href="#category">Add Category</a>
<div class="modal fade" id="category">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add New</h4>
</div>
{!! Form::open(['route' => 'category.store', 'method' => 'post']) !!}
<div class="modal-body">
<div class="form-group">
{{ Form::label('name', 'Title') }}
{{ Form::text('name', null, array('class' => 'form-control')) }}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
{!! Form::close() !!}
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Add
protected $fillable = ['name'];
under your Category class.
And use
Category::create($request->only(['name']));
instead of $request->all() which tries to write to categories._token column inside your Category model combined with create().
You should try this:
<a class="btn btn-primary pull-right navbar-right" data-toggle="modal" href="#category">Add Category</a>
<div class="modal fade" id="category">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add New</h4>
</div>
{!! Form::open(['route' => 'category.store', 'method' => 'post']) !!}
{!! csrf_field() !!}
<div class="modal-body">
<div class="form-group">
{{ Form::label('name', 'Title') }}
{{ Form::text('name', null, array('class' => 'form-control')) }}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
{!! Form::close() !!}
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
you can also add
protected $guarded = [];
to your model and then you can use mass assignment.

delete project with modal in laravel

I have a delete button beside my projects that shows a popup modal for confirmation whenever the button is clicked on. I want the yes button in the modal to then delete the project whenever this button is clicked on.
I'm calling the modal into the projects/show view with #include. The modal does appear when the button is clicked using the following jquery but the yes button when pressed is not deleting the project.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".delete").on("submit", function(e){
e.preventDefault();
$('#myModal').modal('show');
});
});
The delete button beside projects:
{!! Form::open(['route' => ['projects.destroy', $projects->id], 'class' => 'delete', 'method' => 'DELETE']) !!}
{!!Form::button('<span class="glyphicon glyphicon-remove"></span>', ['class' => 'btn btn-danger btn-md', 'type' => 'submit'])!!}
{!! Form::close() !!}
Modal code:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-md" 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 text-center" id="myModalLabel">Delete Confirmation</h4>
</div>
<div class="modal-body text-center">
Are you sure you want to delete this project?
</div>
<div class="modal-footer">
<button type="submit" id="delete-btn" class="btn btn-default" >Yes</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
ProjectController:
<?php
namespace App\Http\Controllers;
use App\project;
use App\Http\Requests;
use Illuminate\Http\Request;
use Session;
class ProjectsController extends Controller
{
public function destroy($id){
$project = Project::find($id);
$project->delete();
Session::flash('success', 'The project was successfully deleted!');
return redirect()->route('projects.show', $project->project_id);
}
}
Change the submit button in your modal to this:
{!! Form::open(['route' => ['projects.destroy', $projects->id], 'class' => 'delete', 'method' => 'DELETE']) !!}
<button type="submit" id="delete-btn" class="btn btn-default" >Yes</button>
{!! Form::close() !!}
The best solution is to remove your implementation of the delete button you have now and make it a normal button. Let that button pop up the modal and replace your "Yes" button with an actual remove button like I did above.

Delete record with bootstrap modalbox confirmation using codeigniter

I want to delete my record from database by giving an alert box which is bootstrap modal box.
i am using codeigniter, i tried but it did not worked. plz Help..
Here is my Controller:
function deleteImage($id = NULL){
$this->config_mdl->delete_image($id);
$this->session->set_flashdata('msg', 'Image Deletion Successful !!');
}
Here is my Model:
function delete_image($id)
{
return $this->db->delete('tbl_gallery', array('image_id' => $id));
}
Here is my View:
<div id="confirmDelete" class="modal fade" role="dialog" aria-lebelledby="confirmDeleteLebel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Confirmation</h4>
</div>
<div class="modal-body">
<p>Are you sure want to delete this record?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-sm" id="confirm">OK</button>
<button type="button" class="btn btn-warning btn-sm" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<div class="caption img-gallery-caption">
<?php
$attributes = array('data-toggle' => 'modal', 'data-target' => '#confirmDelete', 'data-title' => 'Delete Image', 'data-message' => 'Are you sure you want to delete this Image?');
echo anchor('config/editImage', '<i class="glyphicon glyphicon-edit"></i>', $attributes);
echo anchor('config/deleteImage/'.$image->image_id, '<i class="glyphicon glyphicon-trash"></i>');
?>
</div>
Here is my Javascript code:
<script type="text/javascript">
$("#confirmDelete").on('show.bs.modal', function(e){
$(this).find('#confirm').attr('href', $(e.relatedTarget).data('href'));
});
</script>
First ensure that you can go in inside the $("#confirmDelete").on...
You can use ajax in the call
$("#confirmDelete").on('show.bs.modal', function(e){
// Search the 'id'
$.post(
'deleteImage/'+id,
'',
funcion(data){
// some data tha you want to receive from the server
},
'json');
});
you are setting href value of button! button doesn't have href attribute so instead convert your button into anchor tag and it will work!

Categories