This topic is locked

Guide 52 - Using "sweetalert2" to confirm "save" data

2/26/2023 2:02:15 PM
PHPRunner Tips and Tricks
fhumanes author

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.
img alt
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.

C
cristi 2/27/2023

Great!
One quick tip if I may: if you find Sweet alert 2 modal size too small add this to your page css:
.swal2-popup { font-size: 1.6rem !important; }img alt
img alt

fhumanes author 3/1/2023

Hi @cristi.
Thank you very much for your suggestion.
Greetings,
fernando

jadachDevClub member 3/5/2023

Thanks for sharing Fernando. Very useful!!
I tried doing this before without success.

fhumanes author 3/6/2023

Thank you very much @jadach