I found this Ajax for pagination without refreshing the page.
Pagination works, but page is refreshing every time, it's annoying because table is on bottom of the page.
Can you check, maybe I made some mistake. Or give me some ideas to fix this.
Controller:
public function index(Request $request)
{
$satwork = DB::table('companies')
->leftJoin('devices', 'companies.id', '=', 'devices.companyId')
->leftJoin('vehicles', 'devices.id', '=', 'vehicles.deviceId')
->leftJoin('drivers', 'vehicles.id', '=', 'drivers.vehicleId')
->select('companies.company_name', 'devices.device_type', 'vehicles.license_plate', 'drivers.driver_name')
->paginate(5);
return view('/welcome', compact('satwork'));
}
public function fetch_data(Request $request)
{
if ($request->ajax())
{
$satwork = DB::table('companies')
->leftJoin('devices', 'companies.id', '=', 'devices.companyId')
->leftJoin('vehicles', 'devices.id', '=', 'vehicles.deviceId')
->leftJoin('drivers', 'vehicles.id', '=', 'drivers.vehicleId')
->select('companies.company_name', 'devices.device_type', 'vehicles.license_plate', 'drivers.driver_name')
->paginate(5);
return view('pagination', compact('satwork'))->render();
}
}
welcome.blade
<div class="container">
<div id="table_data">
#include('pagination')
</div>
</div>
</div>
<!-- pagination -->
<script>
$(document).ready(function() {
$(document).on('click', '.pagination a', function(event) {
event.preventDefault();
var page = $(this).attr('href').split('page=')[1];
fetch_data(page);
});
function fetch_data(page) {
$.ajax({
url: "//pagination?page=" + page,
success: function(satwork) {
$('#table_data').html(satwork);
}
});
}
});
}
</script>
pagination blade
<tbody>
#foreach ($satwork as $row)
<tr>
<td>{{ $row -> company_name}}</td>
<td>{{ $row -> device_type}}</td>
<td>{{ $row -> license_plate}}</td>
<td>{{ $row -> driver_name}}</td>
</tr>
#endforeach
</tbody>
{!! $satwork->links() !!}
routes
Route::get('/', 'WelcomeController#index');
Route::get('/welcome/pagination', 'WelcomeController#fetch_data');
Your ajax request URL to fetch the pagination is not correct.
url: "//pagination?page=" + page
That should be url: "/welcome/pagination?page=" + page
function fetch_data(page) {
var l = window.location;
// the request path should be
// domain.com/welcome/pagination
$.ajax({
url: l.origin + l.pathname + "?page=" + page,
success: function(satwork) {
$('#table_data').html(satwork);
}
});
}
It can be done with dataTables
Script:
<script>
$(document).ready(function() {
$('#companies').DataTable({
"lengthMenu": [[2, 3, 5, 10, 20, -1], [2, 3, 5, 10, 20, "All"]]
});
});
</script>
Style:
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
And it's working.
Related
I am using laravel 7. In my CRUD application, I am having trouble deleting a user record. I have tried a variety of methods which I have left commented out so that you can see what I tried. I am using Sweetalert2 for the confirmation call and when I click confirm, it redirects to the correct page but I do not get a success message nor is the record deleted from the database. I am new to Laravel so if anyone can spot where I may have gone wrong, I would sure appreciate it.
Here are the following relevant codes.
(EDIT OF CODE BELOW!!!)
index.blade.php
#extends('layouts.admin')
#section('content')
<div class="container-fluid mt-5">
<!-- Heading -->
<div class="card mb-4 wow fadeIn">
<!--Card content-->
<div class="card-body d-sm-flex justify-content-between">
<h4 class="mb-2 mb-sm-0 pt-1">
Inicio
<span>/</span>
<span>Registered Users</span>
</h4>
</div>
</div>
<!-- Heading -->
<!--Grid row-->
<!--Grid column-->
<div class="row">
<!--Card-->
<div class="col-md-12 mb-4">
<!--Card content-->
<div class="card">
<!-- List group links -->
<div class="card-body">
<h5>Users Online:
#php $u_total = '0'; #endphp
#foreach ($users as $user)
#php
if($user->isUserOnline()) {
$u_total = $u_total + 1;
}
#endphp
#endforeach
{{ $u_total }}
</h5>
<table id="datatable1" class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th class="text-center">Online/Offline</th>
<th class="text-center">Banned/Unban</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<input type="hidden" name="id" value="{{ $user->id }}">
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->role_as }}</td>
<td>
#if($user->isUserOnline())
<label class="py-2 px-3 badge btn-success" for="">Online</label>
#else
<label class="py-2 px-3 badge btn-warning" for="">Offline</label>
#endif
</td>
<td>
#if($user->isBanned == '0' )
<label class="py-2 px-3 badge btn-primary">Not Banned</label>
#elseif($user->isBanned == '1')
<label class="py-2 px-3 badge btn-danger">Banned</label>
#endif
</td>
<td>
<a class="badge badge-pill btn-primary px-3 py-2" href="{{ url('role-edit/'.$user->id) }}">Editar</a>
<a class="delete badge badge-pill btn-danger px-3 py-2" data-toggle="modal" href="{{ url('user-delete/'.$user->id) }}" data-target="#delete" data-id="{{ $user->id }}">Borrar</a>
</td>
</tr>
#endforeach
</tbody>
</table>
{{-- <div>
{{ $users->links() }}
</div> --}}
</div>
<!-- List group links -->
</div>
</div>
<!--/.Card-->
</div>
<!--Grid row-->
</div>
#endsection
#section('scripts')
<script>
$(document).ready(function() {
$(document).on('click', '.delete', function (e) {
e.preventDefault();
var id = $(this).data('id');
swal.fire({
title: "¿Estás Seguro/a?",
text: "¡No podrás revertir esto!",
icon: 'warning',
showCancelButton: true,
cancelButtonText: 'Cancelar',
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Sí, Borralo!'
},
function() {
$.ajax({
type: "POST",
url: "{{url('/user-delete/{id}')}}",
data: {id:id},
success: function (data) {
//
}
});
});
});
});
</script>
#endsection
Within my web.php (relevant code only)
Route::group(['middleware' => ['auth', 'isAdmin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('registered-user', 'Admin\RegisteredController#index');
Route::delete('/user-delete/{id}', 'Admin\RegisteredController#destroy');
Route::get('registered-empresa', 'Admin\EmpresaController#index');
Route::get('registered-empleado', 'Admin\EmpleadoController#index');
Route::get('role-edit/{id}', 'Admin\RegisteredController#edit');
Route::put('role-update/{id}', 'Admin\RegisteredController#updaterole');
Route::post('save-empresa', 'Admin\EmpresaController#store');
Route::get('/edit-empresa/{id}', 'Admin\EmpresaController#edit');
Route::put('/empresa-update/{id}', 'Admin\EmpresaController#update');
Route::get('/edit-empleado/{id}', 'Admin\EmpleadoController#edit');
Route::put('/empleado-update/{id}', 'Admin\EmpleadoController#update');
Route::post('save-empleado', 'Admin\EmpleadoController#store');
});
RegisteredController.php (delete function only)
public function destroy(Request $request, $id)
{
// $user = User::findOrFail($id)->delete();
// return redirect()->back()->with('status', 'Usuario ha sido borrado.');
// $user = User::find($id);
// $user->delete();
// return redirect()->back()->with('status', 'Usuario ha sido borrado.');
User::destroy($id);
}
EDIT!!!
I changed the way I am calling the sweetalert2 but am still getting errors. I changed my delete button in index.blade.php to
<a class="delete badge badge-pill btn-danger px-3 py-2" onclick="deleteConfirmation({{ $user->id }})">Borrar</a>
In the same file, I changed the script to the following:
function deleteConfirmation(id) {
swal.fire({
title: "Borrar?",
text: "¿Estás seguro/a?",
icon: "warning",
showCancelButton: !0,
confirmButtonText: "Sí,Borralo!",
cancelButtonText: "Cancelar",
reverseButtons: !0
}).then(function (e) {
if (e.value === true) {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$.ajax({
type: 'POST',
url: "{{url('/user-delete')}}/" + id,
data: {_token: CSRF_TOKEN},
dataType: 'JSON',
success: function (results) {
if (results.success === true) {
swal.fire("Done!", results.message, "success");
} else {
swal.fire("Error!", results.message, "error");
}
}
});
} else {
e.dismiss;
}
}, function (dismiss) {
return false;
})
}
I also removed the .ready function as it was causing a conflict with sweetalert2
My Delete function in my RegisteredController now looks like this:
public function delete($id)
{
$delete = User::where('id', $id)->delete();
// check data deleted or not
if ($delete == 1) {
$success = true;
$message = "Usuario borrado exitosamente";
} else {
$success = true;
$message = "Usuario no encontrado";
}
// Return response
return response()->json([
'success' => $success,
'message' => $message,
]);
}
}
And in my web.php, I changed the route to:
Route::post('/user-delete/{id}', 'Admin\RegisteredController#delete');
The sweet alert now pops up and when I click delete, I get the console error:
POST http://ecomsivendo.test/user-delete/4 419 (unknown status)
You also need the headers section for jquery ajax calls.
$.ajax({
type: "POST",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
//.......
Also make sure you have the meta tag in the <head> on the page.
<meta name="csrf-token" content="{{ csrf_token() }}">
Thank you for all those who tried to help me on this. I ended up giving the code another overhaul and this is what I had to do to get it to work.
I had to change my ajax request in the index.blade.php
function deleteConfirmation(id) {
swal.fire({
title: "Borrar?",
text: "¿Estás seguro/a?",
icon: "warning",
showCancelButton: true,
confirmButtonText: "Sí,Borralo!",
cancelButtonText: "Cancelar"
}).then(function (e) {
if (e.value === true) {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$.ajax({
type: 'POST',
url: "{{url('/user-delete')}}/" + id,
headers: {
'X-CSRF-TOKEN': '<?php echo csrf_token() ?>'
},
data: {"id": id, _method: 'delete'},
dataType: 'JSON',
success: function (results) {
if (results.success === true) {
swal.fire("¡Hecho!", results.message, "success");
setTimeout(function() {
location.reload();
}, 1000);
} else {
swal.fire("¡Error!", results.message, "error");
}
}
});
} else {
e.dismiss;
}
}, function (dismiss) {
return false;
})
}
I am not happy with the setTimout function to get the page to reload and if someone can offer a better solution to avoid reload, I would really appreciate it.
in the controller, I had to change the delete function to the following:
public function delete($id)
{
$delete = User::where('id', $id)->delete();
// check data deleted or not
if ($delete == 1) {
$success = true;
$message = "Usuario borrado exitosamente";
} else {
$success = true;
$message = "Usuario no encontrado";
}
// Return response
return response()->json([
'success' => $success,
'message' => $message,
]);
}
And finally in my web.php, I have the route as:
Route::delete('/user-delete/{id}', 'Admin\RegisteredController#delete');
like I mentioned earlier, I feel like the setTimeout is a hack and I would prefer something a bit more elegant.
In the end, the code does what it is supposed to do and that is delete the entry and update the view.
I have taken an example from the internet of working laravel with ajax. But it gives me the 500 internal server error:
jquery.min.js:4 GET 127.0.0.1:8000/search?search=p 500 (Internal Server Error) send # jquery.min.js:4 ajax # jquery.min.js:4 (anonymous) # (index):39 dispatch # jquery.min.js:3 r.handle # jquery.min.js:3
This is the code for controller called SearchController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function index()
{
return view('search.search');
}
public function search(Request $request)
{
if($request->ajax())
{
$output="";
$products=DB::table('products')->where('title','LIKE','%'.$request->search."%")->get();
if($products)
{
foreach ($products as $key => $product) {
$output.='<tr>'.
'<td>'.$product->id.'</td>'.
'<td>'.$product->title.'</td>'.
'<td>'.$product->description.'</td>'.
'<td>'.$product->price.'</td>'.
'</tr>';
}
return Response($output);
}
}
}
}
Code in web.php
Route::get('/','SearchController#index');
Route::get('/search','SearchController#search');
Code of blade file
<!DOCTYPE html>
<html>
<head>
<meta id="token" name="_token" content="{{ csrf_token() }}">
<title>Live Search</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Products info </h3>
</div>
<div class="panel-body">
<div class="form-group">
<input type="text" class="form-controller" id="search" name="search"></input>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$('#search').on('keyup',function(){
$value=$(this).val();
$.ajax({
type : 'get',
url : '{{URL::to('search')}}',
data:{'search':$value},
success:function(data){
$('tbody').html(data);
}
});
});
</script>
<script type="text/javascript">
$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>
</body>
</html>
It should display the record when the key is pressed but it gives error 500 internal server error
Screenshot with GET method
AJAX ERROR WITH GET
Screenshot with POST method
AJAX ERROR WITH POST
Try this
<script type="text/javascript">
$('#search').on('keyup',function(){
value=$(this).val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
$.ajax({
url : '{{ url('search') }}',
method : 'POST',
data:{'search': value},
success:function(response){
console.log(response.data)
}
});
})
</script>
your route
Route::post('/search','SearchController#search');
and in your controller, verify
public function search(Request $request)
{
$products=DB::table('products')->where('title','LIKE','%'.$request->search."%")->get();
return response()->json(['data' => $products]);
}
The problem is, There is no DB imported.
So Just write
use DB;
in the controller
I have a search field with the same name and id inside my categories page and inside my products page.The autocomplete suggestions seems to work fine , however once I click on requested product inside the search field, it's stays on the same page and not redirecting me to the view.I want my view to show only products. This is my code so far:
After update
My routes:
<?php
Route::get('products/{id}', 'AutoCompleteController#show');
Route::get('autocomplete', array('as' => 'autocomplete', 'uses' => 'AutoCompleteController#show'));
Route::get('searchajax', array('as' => 'searchajax', 'uses' => 'AutoCompleteController#autoComplete'));
My AutoCompleteController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\MainController;
use App\Product;
class AutoCompleteController extends MainController
{
public function show(Product $product)
{
return view('content.products', ['product' => $product]);
}
public function autoComplete(Request $request)
{
$query = $request->get('term', '');
$products = Product::where('title', 'LIKE', '%' . $query . '%')->get();
$data = [];
foreach ($products as $product) {
$data[] = array('label' => $product->title, 'value' => $product->id);
}
if (count($data)) {
return $data;
} else {
return ['value' => 'No Result Found', 'id' => ''];
}
}
}
My view in products.blade.php and categories.blade.php for my autocomplete search is the same:
#extends('master')
#section('content')
<link href="http://demo.expertphp.in/css/jquery.ui.autocomplete.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div class="row">
<form class="navbar-form text-center " form method="GET" action=" ">
<input id="search_text" placeholder=" Search products" name="search_text" type="text" value=""
style="width: 400px; height: 35px; border-radius: 5px ; padding-left: 12px;"><br><br>
<input class="btn btn-default " type="submit" value=" Search">
</form>
</div>
<script>
$(document).ready(function () {
src = "{{ route('searchajax') }}";
$("#search_text").autocomplete({
source: function (request, response) {
$.ajax({
url: src,
dataType: "json",
data: {
term: request.term
},
success: function (data) {
response(data);
}
});
},
minLength: 3,
select: function (event, ui) {
window.location = '{{ url('shop/{category_url}')}}' + ui.item.id
} // not sure if this is the correct way , please advise
});
});
</script>
#endsection
If you seems the issue with JS conflict, try below code:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
Added missing ui css too. Let me know the result.
There are a few problems:
An autocomplete response should include label and value pairs, you are returning value and id. See the jQuery UI source docs.
Your Javascript is missing the select event handler, which specifies what happens when one of the suggestions is selected. So right now clicking one will just fill the clicked value in the input field. See the jQueryUI autocomplete select docs.
Maybe you want to be able to view product ID 1 when you browse to /products/1. First you need to set up a route for that:
Route::get('products/{id}', 'AutoCompleteController#index');
Then in your controller, first fix the autocomplete response to return the right format:
foreach ($products as $product) {
$data[]=array('label'=>$product->title,'value'=>$product->id);
}
Next, still in the controller, update the method for showing your product (BTW this should probably be called show, index would usually be a list of products:
use App\Product;
class AutoCompleteController extends MainController {
// This assumes you have a Product model
public function index(Product $product) {
return view('content.products', ['product' => $product]);
}
And your Javascript:
$("#search_text").autocomplete({
source: function (request, response) {
$.ajax({
url: src,
dataType: "json",
data: {
term: request.term
},
success: function (data) {
response(data);
}
});
},
minLength: 3,
select: function( event, ui ) {
// Your autoComplete response returns the ID in the 'value' field
window.location = 'http://yoursite.com/products/' + ui.item.value
}
});
I am using Laravel and Vue Js for the data listing and paginate data using vue component, without using component my code works fine but when i use component pagination bar is working but not sync with listing,
Here is my Html
<!DOCTYPE html>
<html>
<head>
<title>Users List</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css">
</head>
<body>
<div class="container" id="users">
<!-- Item Listing -->
<table class="table table-bordered">
<tr>
<th>Name</th>
<th>Email</th>
<th>Created At</th>
</tr>
<tr v-for="user in users">
<td>#{{ user.name }}</td>
<td>#{{ user.email }}</td>
<td>#{{ user.created_at }}</td>
</tr>
</table>
<vue-pagination></vue-pagination>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/vue.resource/0.9.3/vue-resource.min.js"></script>
<script type="text/javascript" src="/js/users.js"></script>
</body>
</html>
And Here is my Vue Js Code
var VueComponent = Vue.extend({
template:
'<nav>' +
'<ul class="pagination">' +
'<li v-if="pagination.current_page > 1">' +
'<a href="#" aria-label="Previous" #click.prevent="changePage(pagination.current_page - 1)">' +
'<span aria-hidden="true">«</span>' +
'</a>' +
'</li>' +
'<li v-for="page in pagesNumber" :class="{\'active\': page == pagination.current_page}">' +
'{{ page }}' +
'</li>' +
'<li v-if="pagination.current_page < pagination.last_page">' +
'<a href="#" aria-label="Next" #click.prevent="changePage(pagination.current_page + 1)">' +
'<span aria-hidden="true">»</span>' +
'</a>' +
'</li>' +
'</ul>' +
'</nav>',
props: ['user'],
data: function() {
return {
pagination: {
total: 0,
per_page: 2,
from: 1,
to: 0,
current_page: 1
},
offset: 4,
}
},
computed: {
isActived: function () {
return this.pagination.current_page;
},
pagesNumber: function () {
if (!this.pagination.to) {
return [];
}
var from = this.pagination.current_page - this.offset;
if (from < 1) {
from = 1;
}
var to = from + (this.offset * 2);
if (to >= this.pagination.last_page) {
to = this.pagination.last_page;
}
var pagesArray = [];
while (from <= to) {
pagesArray.push(from);
from++;
}
return pagesArray;
}
},
ready : function(){
this.getUsers(this.pagination.current_page);
},
methods : {
getUsers: function(page){
this.$http.get('/user/api?page='+page).then((response) => {
this.$set('pagination', response.data);
});
},
changePage: function (page) {
this.pagination.current_page = page;
this.getUsers(page);
}
}
})
Vue.component('vue-pagination', VueComponent);
new Vue({
el: '#users',
data: {
users: [],
pagination: {
total: 0,
per_page: 2,
from: 1,
to: 0,
current_page: 1
},
offset: 4,
},
ready : function(){
this.getUsers(this.pagination.current_page);
},
methods : {
getUsers: function(page){
this.$http.get('/user/api?page='+page).then((response) => {
this.$set('users', response.data.data);
});
},
}
});
How to make this pagination work with vue js when using vue component Please Help me.
It is not that complicated. You just forgot to link your current page with your component. To do so , just change your code in HTML section
<vue-pagination></vue-pagination>
to
<vue-pagination :pagination="pagination" v-on:click="getUsers(pagination.current_page)"></vue-pagination>
In your js code , get rid of data function from VueComponent and add pagination props
.....
props: {
pagination: {
type: Object,
required: true
}
},
computed: {
pagesNumber: function () {
if (!this.pagination.to) {
......
Also, remove your getUsers method from VueComposent and combine with getUsers method in Vue main instance
getUsers: function(page){
this.$http.get('/user/api?page='+page).then((response) => {
this.$set('users', response.data.data);
this.$set('pagination', response.data);
});
I think, now your code should work as expected.
Hello I have decided to use Bootbox.js as a simple way to incorporate a bootstrap modal so that I can make the user confirm their choice before the form is posted and the changes are made.
I am using Laravel, jQuery, Bootstrap and Bootbox.js in my default layout file:
Layout.blade.php
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"></script>')</script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
<script src="js/vendor/bootstrap.js"></script>
<script src="js/vendor/modernizr-2.6.2.min.js"></script>
<script src="js/vendor/bootbox.js"></script>
User Page:
{{Form::open('choice')}}
#foreach($items as $item)
<tr>
<td>{{ HTML::image($item->image, '', array('class' => 'img img-rounded', 'width' => '100px', 'height' => '100px')) }}</td>
<td>{{$item->name}}</td>
<td>{{ $item->desc }}</td>
<td>{{ Form::radio('item', $item->item_id)}}</tr>
#endforeach
</tbody>
</table>
{{ Form::submit('Confirm Choice', array('class' => 'btn btn-success btn-block')) }}
<script>
$(document).ready(function() {
$(document).on("click", ".alert", function(e) {
bootbox.confirm("Are you happy with the decision you have made?", "I have changed my mind", "I am happy with my choice", function(result) {
if (result) {
console.log("user confirmed");
} else {
console.log("user declined");
}
});
});
});
</script>
{{Form::close()}}
I do not know how to make the modal appear before and prevent submission. But only post if the user has confirmed their choice?
Adding e.preventDefault() in the onClick handler will prevent the form submission.
<script>
$(document).ready(function() {
$(document).on("click", ".alert", function(e) {
e.preventDefault();
bootbox.confirm("Are you happy with the decision you have made?", "I have changed my mind", "I am happy with my choice", function(result) {
if (result) {
console.log("user confirmed");
} else {
console.log("user declined");
}
});
});
});
</script>