After Clicking a Button of Sweet Alert Call PHP File

after clicking a button of sweet alert call php file

This would help you. It can take you to page_logout.php
And there is also some error in your swal code which you have written in

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<a href="#" id="a_id">Logout</p>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#a_id").click(function(){

swal({title:'Logout',
text:'Do you want to logout this Account?',
icon:'warning',
buttons: true,
dangerMode: true
})
.then((willOUT) => {
if (willOUT) {
window.location.href = 'page_logout.php', {
icon: 'success',
}
}
});

});
});
</script>
</body>
</html>

or if you want to show message in the same page in which there is logout button, then instead of url in your question do ajax request to page_logout.php. Hope it will help you.

How to redirect page after click on Ok button on sweet alert?

To specify a callback function, you have to use an object as the first argument, and the callback function as the second argument.

echo '<script>
setTimeout(function() {
swal({
title: "Wow!",
text: "Message!",
type: "success"
}, function() {
window.location = "redirectURL";
});
}, 1000);
</script>';

Redirect to a web page when a user clicks on the ok button of the sweet alert

Manual

Swal.fire({
title: 'Welcome to bottles beach',
text: "You successfully submitted the form",
icon: 'success',
showCancelButton: false,
confirmButtonColor: '#3085d6',
confirmButtonText: 'Great, show me the site!'
}).then((result) => {
if (result.isConfirmed) {
location = "somewhereelse.html"
}
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10.15.6/dist/sweetalert2.all.min.js"></script>

How to reload a page after clicked ok using sweetalert

You can try this, it works for me.

swal({
title: "Good job",
text: "You clicked the button!",
type: "success"
},
function(){
location.reload();
}
);

Sweet Alert and ajax calls based on id on php while loop

Step 1, add a class to buttons, for example user-details and remove attribute onclick. Also add data-* attributes with your details, example:

<button class='btn btn-success user-details' data-name="John Doe 1" data-image="https://png.icons8.com/contacts/ios7/50/e67e22">View Details</button>

Step 2, bind an event handler to these buttons, note these attributes being used (you can add how many you want):

$('.user-details').click(function(e) {
e.preventDefault();

var name = $(this).attr('data-name');
var image = $(this).attr('data-image');

swal({
title: "User Details",
text: "User Details: " + name,
confirmButtonColor: "#00B4B4",
imageUrl: image,
});
});

This is pretty all you need, you can try a demo here.

How to send custom value to sweetalert javascript code from PHP?

Add data-fileId attribute to your button and when you click to button, get clicked buttons 'data-fileId'. it should be like this

html:

<button type="button" class="btn btn-primary" id="custom-sa-warning" data-fileId="write your fileId">Click me</button>

js:

<script>
document.getElementById("custom-sa-warning") && document.getElementById("custom-sa-warning").addEventListener("click", function(event) {
var el = event.target || event.srcElement;
var fileId = el.getAttribute('data-fileId');
Swal.fire({
html: '<div class="mt-3"><lord-icon src="https://cdn.lordicon.com/gsqxdxog.json" trigger="loop" colors="primary:#f7b84b,secondary:#f06548" style="width:100px;height:100px"></lord-icon><div class="mt-4 pt-2 fs-15 mx-5"><h4>Are you Sure ?</h4><p class="text-muted mx-4 mb-0">Are you Sure You want to Delete this Account ?</p></div></div>',
showCancelButton: !0,
confirmButtonClass: "btn btn-primary w-xs me-2 mb-1",
confirmButtonText: "Yes, Delete It!",
cancelButtonClass: "btn btn-danger w-xs mb-1",
buttonsStyling: !1,
showCloseButton: !0
}).then((result) => {
if (result.value) {
window.location.href = "https://somedimain.com/delete.php?id=" + fileId
}
});
})
</script>

Delete a file with php and sweet alert 2

Problem

Is that you have many duplicated js functions <script>swal({})</script> bound to one selector .unlink a. You are adding a duplicate sweet alert function for each file that can be deleted, since they are all bound to the same selector the first one is executed when you click any delete link. Whichever file is referenced in that function gets deleted.

Solution

Instead of duplicating this sweet alert function inside the foreach loop you are using to generate the links. You could add a data tag to each link with the filename and then have one js function responsible for the handling the delete. Something like this:

foreach (array_keys($files) as $file) {
//add whatever filename modifications here
echo '<a href="#" class="unlink" data-file="'.$file.'">Delete</a>';
}

<script>
$("a.unlink").on("click", function(e) {
// Run the Sweet Alert 2 code.
swal({
title: "Radera denna bild?",
text: "Klicka på Ja om du vill radera denna bild.",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#d33",
cancelButtonColor: "#3085d6",
confirmButtonText: "Ja, Radera!",
cancelButtonText: "Nej, Avbryt!",
}).then((result) => {
if (result.value) {
// If confirm run the unlink.php file.
window.location.href ="unlink.php?filename="+$(this).data('file');
} else if (
result.dismiss === swal.DismissReason.cancel
) {
// If cancel do nothing (stay on same page).
}
})
})</script>


Related Topics



Leave a reply



Submit