I Have Used The Sweet Alert For Delete using AJAX, And I set Page redirection after Deleteing. Whenever I Click delete, It asks Confirm or not buttons,
If i click Yes-Confirm it will delete,and the Page Was Redirected. I Want
like After deleteing success message "OK Successfully Deleted" That time only page Was Refreshed,
function delete_confirm(id) {
swal({
title: "Are you sure?",
text: "You will not be able to recover this Product Attribute!",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes, delete it!',
cancelButtonText: "No, cancel please!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
$.ajax({
url: "cart-delete.php",
type: "POST",
data: {
id: id,
},
success: function(data) {
console.log(data);
swal("Done!", "It was succesfully deleted!", "success");
$('[remove_attr_' + id + ']').parents('.demo-card-wide').remove();
window.location.href = 'add-to-cart.php';
}
});
} else {
swal("Cancelled", "Your Product Attribute! is safe :)", "error");
}
});
};
Remove this line On Success
window.location.href = 'add-to-cart.php';
Write in some other action
ex. On sweet alert close action
Related
I'm trying to create a page named 'list of users' where I have a sweetalert with input text form and user enters a name in sweetalert and then using a POST method like a form to redirect to mywebsite.com/search and show results with that name.
Sweetalert is putted on homepage.
<script>
function searchp()
{
Swal({
title: 'Search',
input: 'text',
inputPlaceholder: 'enter_name',
inputAttributes: {
autocapitalize: 'off'
},
showCancelButton: true,
confirmButtonText: 'Search',
confirmButtonColor: '#ffffff',
cancelButtonColor: '#d33'
}).then(result => {
if (result.value) {
$.ajax({
type: "POST",
url: '<?php echo base_url("search"); ?>',
data: {
//here 1
},
success: function(data)
{
//here 2
}
});
} else {
Swal(
'Ops!',
'You canceled.',
'error'
)
}
})
}
</script>
As you saw I don't know how to apply POST method on ''. I'm using Codeigniter 3 as php framework.
is equal to mywebsite.com/search.
I want after a user entered name in input form and pressed submit to apply post method on search page and show results there. The backend for search page is already made and is working with a normal tag.
Hey if you want to redirect the page then you don't need ajax request at all. First get the input value and then use it as a parameter in your wanted URL. I've created a demo, see if this works for you. ↓↓
function searchp() {
Swal.fire({
title: 'Search',
input: 'text',
inputPlaceholder: 'Enter Name',
inputAttributes: {
autocapitalize: 'off'
},
showCancelButton: true,
confirmButtonText: 'Search',
//confirmButtonColor: '#eee',
cancelButtonColor: '#d33',
preConfirm: (data) => {
if (data != "") {
// window.location = '<?php echo base_url('search')?>' + data;
// or(whichever works for you)
// window.location = '<?php echo base_url('search')?>' + '/' + data;
console.log(`Your name: ${data}`)
} else {
Swal.showValidationMessage(`Please enter name`)
}
},
})
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2#9.10.12/dist/sweetalert2.all.min.js"></script>
<button onclick="searchp()" >Fire Sweet Alert</button>
This question already has an answer here:
Display Successful Message Notifications at Sweetalert
(1 answer)
Closed 4 years ago.
I want to display the message "successfully deleting data" with Sweet alert, and the beginning of the script:
Delete
javascript :
jQuery(document).ready(function($) {
$('.delete-link').on('click', function() {
var getLink = $(this).attr('href');
swal({
title: 'Alert',
text: 'Delete?',
html: true,
confirmButtonColor: '#d9534f',
showCancelButton: true,
}, function() {
window.location.href = getLink
});
return false;
});
});
The data has been successfully deleted, but the way i want to display the data message has been deleted like this, where is it put in?
swal("Success!", "Successfully Deleted Data!", "success");
I don't know much about Sweetalert but as far as I guess the 2nd parameter of the swal function is confirm action!!
So you can delete this using jquery ajax after confirming true:
jQuery(document).ready(function($) {
$('.delete-link').on('click', function() {
var getLink = $(this).attr('href');
swal({
title: 'Alert',
text: 'Delete?',
html: true,
confirmButtonColor: '#d9534f',
showCancelButton: true,
}, function() {
// window.location.href = getLink // no redirection or page reload
$.ajax({
url: getLink, // link where your href is
success: function(result) {
swal('Successful!!', 'Successfully deleted!!'); //sweet alert
}
});
});
return false;
});
});
I have made a delete modal with Sweet Alert in Laravel and it is deleting user I choose. I would however after deletion like to redirect back to users list as my destroy() method says.
<script>
$('a#delete_user').on('click', function () {
var url = $(this).attr("data-href");
swal({
title: "Delete user?",
text: "Submit to delete",
type: "warning",
showCancelButton: true,
closeOnConfirm: false,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Delete!"
},
function () {
setTimeout(function () {
$.ajax({
type: "POST",
url: url,
data: {
_method: 'DELETE',
_token: csrf_token
},
success: function (data) {
if (data)
swal("Deleted!", "User has been deleted", "success");
else
swal("cancelled", "User has not been deleted", "error");
}
}), 2000
});
});
})
</script>
Here is the controller:
public function destroy($id)
{
User::destroy($id);
return redirect('users')->with('status', 'User Deleted!');
}
I would like to redirect to users list with message, and I have trouble doing so
In your main template file or in view file, you have to check if there is status data in session, if yes so you call sweet alert. Example code:
#if (session('status'))
<script>
$( document ).ready(
swal("{{ session('status') }}")
});
</script>
#endif
Edit js file like this:
success: function (data) {
if (data == 'success')
swal("Deleted!", "User has been deleted", "success");
window.location('Your URL');
else
swal("cancelled", "User has not been deleted", "error");
}
and the controller:
if(User::destroy($id)){
return 'success';
}else{
return 'fail';
}
Need help in converting PDO array execution to mysqli statements. This executes for an AJAX POST call for deleting a particular id from database with SWAL function.
<?php
require_once 'connectivity.php';
$response = array();
if ($_POST['delete']) {
$pid = intval($_POST['delete']);
$query = "DELETE FROM students WHERE id=$pid";
$stmt = mysqli_query($query,$connect);
$stmt->execute(array(':pid'=>$pid));
if ($stmt) {
$response['status'] = 'success';
$response['message'] = 'Student Deleted Successfully ...';
} else {
$response['status'] = 'error';
$response['message'] = 'Unable to delete ...';
}
echo json_encode($response);
}
?>
This is the consecutive swal function which is to be executed once the delete button clicked.
<script>
$(document).ready(function(){
$(document).on('click', '#deleteid', function(e){
var productId = $(this).data('id');
SwalDelete(productId);
e.preventDefault();
});
});
function SwalDelete(productId){
swal({
title: 'Are you sure?',
text: "It will be deleted permanently!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!',
showLoaderOnConfirm: true,
preConfirm: function() {
return new Promise(function(resolve) {
$.ajax({
url: 'delete.php',
type: 'POST',
data: {'delete' : productId}
})
.done(function(response){
swal('Deleted!', response.message, response.status);
readDetails();
})
.fail(function(){
swal('Oops...', 'Something went wrong with ajax !', 'error');
window.location.reload(true);
});
});
},
allowOutsideClick: false
});
}
function readDetails(){
window.location.reload(true);
}
Hello I have a code using sweetalert
swal("Good job!", "You clicked the button!", "success")
this code will pop-up a message and has a button okay, what I like to do is I want to refresh the page after I click the okay button.
Can I do that?
You can try this, it works for me.
swal({
title: "Good job",
text: "You clicked the button!",
type: "success"
},
function(){
location.reload();
}
);
The answer from Yoshioka did not work for me, I did this and it worked perfectly:
swal({title: "Good job", text: "You clicked the button!", type:
"success"}).then(function(){
location.reload();
}
);
Use the callback function...
Swal.fire({
// Swal Setting's
}).then((result) => {
// Reload the Page
location.reload();
});
For Sweet Alert 2, this will work.
swal("Good job!", "You clicked the button!", "success").then(function(){
location.reload();
});
As you can see migration guide
Sweet Alert 2 uses Promise
You can check confirm by this:
swal({
title: "Good job",
text: "You clicked the button!",
icon: "success",
buttons: [
'NO',
'YES'
],
}).then(function(isConfirm) {
if (isConfirm) {
location.reload();
} else {
//if no clicked => do something else
}
});
I use sweet alert 2 and this works for me
swal("Good job!", "You clicked the button!","success").then( () => {
location.href = 'somepage.html'
})
‘’’
The answers making use of location.reload() is going to trigger your form to attempt to resubmit over and over again, thats why you should use location.href instead.
In Sweet Alert 2, there is callback function where you can implement your logic :
Swal.fire({
title: 'Great job',
text: "You clicked the button!",
type: 'success',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes'
}).then((result) => {
if(result){
// Do Stuff here for success
location.reload();
}else{
// something other stuff
}
})
You can find the reference on SweetAlert2.
Swal.fire(
{
title: "Good job",
text: "You clicked the button!",
type: "success",
showDenyButton: true, // In case you want two scenarios
denyButtonText: 'ABC',
showCancelButton: true, // In case you want two scenarios
cancelButtonText:'XYZ'
}
).then(function (result) {
if (result.isConfirmed) {
//You can add code here if user pressed ok button
} else if (result.isDenied) {
//You can add code here if user pressed deny button
} else if(result.isDismissed) {
//You can add code here if user pressed cancel button
}
)
this worked for me.
Swal.fire('Deleted !!', data.message, 'success').then(() => {
location.reload();
});
swal({
title: "Process Completed",
text: "Data Recorded successfully",
buttons: true
}).then(function(){
location.reload();
});
i solved it with this :
swal({title: "Updated!", text: "yourText", type: "success"},function() {
location.reload();
}) // swal end
Another way :
swal("Updated!","yourText","success",function() {
location.reload();
}) // swal end