One of the PHPRunner developers asked me how he could use the "Sweetalert2" dialog API before saving the form data.
I was studying how to do it including code in the "Javascript Onload event" event and I didn't see a clear way that would work in all cases (popup pages included).
Aim
Use the “Sweetalert2” dialog API before saving the form data.
DEMO: https://fhumanes.com/sweetalert2/
Technical Solution.
The solution is to put in the forms ( ADD or EDIT ) an additional "custom" button of "SAVE", but totally programmed by us.
And from there 2 JavaScript are created.
- In "Javascript Onload Event":
$('a[id^="saveButton"]').hide(); // Hide button "Save"
- In “ Client Before ” of the “custom” button:
`Swal.fire({
title: 'Do you want to save the changes?',
showDenyButton: true,
// showCancelButton: true,
confirmButtonText: 'Save',
denyButtonText: "Don't save",
}).then((result) => {
/ Read more about isConfirmed, isDenied below /
if (result.isConfirmed) {
Swal.fire('Saved!', '', 'success')
$('a[id^="saveButton"]').click();
} else {
if (result.isDenied) {
Swal.fire('Changes are not saved', '', 'info')
}
}
})
return false;`Where the dialogs are included and when appropriate, "click" on the standard "Save" button to execute the actions to save the data.
In this simple way, you can validate the additional fields and use the power and attractiveness of the new dialog with "Sweetalert2".
For any doubt or clarification, send it to me through my email fernandohumanes@gmail.com
I leave you the sample project so that you can install it on your PC and make as many corrections or tests as you require.