SweetAlert2 - after refresh href not working - javascript

I have this thing. After I refresh the page the script is stop working.
How do I need to do it to be working?
<a onclick="confirmation(event)" href="{{ url('trash/delete/'.$items->id) }}"> Delete</a>
This is the script
<script>
function confirmation(e) {
e.preventDefault();
var url = e.currentTarget.getAttribute('href')
Swal.fire({
icon: 'warning',
title: 'Are you sure?',
text: 'This action cannot be undone!',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, recall!'
}).then((result) => {
if (result.value) {
window.location.href=url;
}
})
}
</script>
i have try this : location.reload(); in multilocation, but not working

your code works for me using this library
<script src="https://cdn.jsdelivr.net/npm/sweetalert2#11"></script>
can you try with this and see if it will work?

Related

Sweet alert wait for reaction

Alert does not wait for user response and the form completes, Does anyone know how to solve this problem?
I need it in response to a button that is not on the form because I have several buttons on the form. Thank you
function Upravit(){
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
);
return true;
} else {
return false
}
});
}
<button {if $d->aktivni == 0}disabled{/if} onclick="return Upravit()"
n:name="upravitreklamaci" type="submit" class="btn btn-primary btn-lg btn-block">Upravit
reklamaci</button>

add sweetalert2 confirm delete in php without json data

How to use sweetalert2 with delete function in button href ?
i have button like this
<button type="button" id="btnDelete" href=" <?php echo site_url('administrator/master/delete/' . $a->idDept); ?>" class="btn btn-danger fa fa-trash but" data-toggle="tooltip" data-placement="top" title="Delete"></button>
$('.btnDelete').click(function() {
swal.fire({
title: 'Are you sure?',
text: "It will permanently deleted !",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(function() {
swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
);
});
});
sweetalert2 working but not delete the data
You will need to pass the fetch/ajax method in Swal.fire call.
$('.btnDelete').click(function(e) {
e.preventDefault();
var url = $(this).attr('href');
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
preConfirm: () => {
return fetch(url)
.then(response => {
console.log(response);
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json()
})
.catch(error => {
Swal.showValidationMessage(
`Request failed: ${error}`
)
})
}
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
}
});
});
Use this example codepan and modify it as you need.
https://codepen.io/ympervej/pen/wvJQyOr

How to use Sweetarlet2 delete?

I have an issue, I'm learning how to use sweetarlet2, I'm using spring project. I want to make a button that have arlet before delete it.
here my HTML button.
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#delete" onclick="deleteArlet('+${ps.nama}+ ')"> delete </button>
and here my js script.
<script>
function deleteArlet(id){
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
url: "/siswa/delete/"+ Id,
data: { Id: Id }
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
}
})
}
try, if(result.isConfirmed) instead of if (result.value). that worked for me

Using SweetAlert2 in vue js to make a modal confirmation before deleting the item

I have an error in my sweetalert2 and I am using laravel vue in developing my app. What I want my app to happen is to create a confirmation modal for deleting a row in my database. Whenever I click "Yes", the item is removed but when I click the cancel button, the modal closes but also deletes the entire row. I am very confused as of the moment and this is my first time learning these frameworks and I want to learn more about this.
this is my code under the IndexComponent.vue
methods: {
deletePost(id) {
this.$swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
closeOnCancel: true
}).then((result) => {
//send request to server
let uri = `/api/post/delete/${id}`;
axios.delete(uri).then(response => {
this.posts.splice(this.posts.indexOf(id), 1);
});
if (result.value) {
this.$swal(
'Deleted!',
'Your post has been deleted!',
'success'
)
}
})
}
}
This is my button placed inside a td in my table:
<td><button #click="deletePost(post.id)" class="btn btn-danger">Delete</button></td>
This is what's inside my PostController.php:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Resources\PostCollection;
use App\Post;
public function delete($id) {
$post=Post::find($id);
$post->delete();
return response()->json('Successfully deleted!');
}
All operations are working (CRUD) but when I tried to implement the sweetalert2 the deletions are multiple. Can someone please help me?
You have to write your API call inside if like this
methods: {
deletePost(id) {
this.$swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
closeOnCancel: true
}).then((result) => {
//send request to server
if (result.value) {
let uri = `/api/post/delete/${id}`;
axios.delete(uri).then(response => {
this.posts.splice(this.posts.indexOf(id), 1);
});
this.$swal(
'Deleted!',
'Your post has been deleted!',
'success'
)
}
})
}
}
Your splice index is incorrect. It will return -1, then it will delete the last item.
It should be
this.posts = this.posts.filter(post => post.id !== id)
Full code
methods: {
deletePost(id) {
this.$swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
closeOnCancel: true
}).then((result) => {
//send request to server
if (result.value) {
let uri = `/api/post/delete/${id}`;
axios.delete(uri).then(response => {
this.posts = this.posts.filter(post => post.id !== id)
this.$swal(
'Deleted!',
'Your post has been deleted!',
'success'
)
});
}
})
}
}

Sweet Alert 2 and javascript:How do i make a Sweet alert button that sends me to a certain webpage when clicked

My code so far:
swal({
title: 'Successfully Registered!',
text: "Do you want to go to the Log In page?",
type: 'success',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, send me there!'
},
function(){
window.location.href= "http://example.com";
}
);
For some reason the window.location.href is not working and i tried using only location.href and if(isConfirm) etc.
What should i do?
JSFiddle:https://jsfiddle.net/2dz868kd/
Here's how you can make it with SweetAlert2:
Swal.fire({
title: 'Successfully Registered!',
text: 'Do you want to go to the Log In page?',
icon: 'success',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, send me there!'
}).then(function(result) {
if (result.isConfirmed) {
location.assign("https://stackoverflow.com/")
}
});
<script src="https://cdn.jsdelivr.net/npm/sweetalert2#11"></script>
SweetAlert2 documentation: https://sweetalert2.github.io/
Please note that SweetAlert2 and SweetAlert are two different projects. SweetAlert2 uses ES6 Promises.
This is what i use to redirect on click (OK)
<script>
sweetAlert({title: 'Error',text: 'Invalid OTP',type: 'error',onClose: function(){
window.location.href = 'https://google.com';
}});
</script>

Categories

Resources