Call javascript onsubmit does not "hold" the Submit until it gives confirmation of shunning

Asked

Viewed 740 times

1

I find the following mistake:

<form action="" method="post" onsubmit="sweetalert(1)">
            Dados aqui !!!

            <input type="button" id="selectall-game-button" label="check all" value="Selecionar tudo">
            <input type="submit" id="delete-game-button" value="Eliminar" style="display:none;"/>

JS

function sweetalert(x) {
    switch (x) {
        case 1:
            swal({
                title: "Aviso!",
                text: "Tens a certeza que queres apagar este/s ficheiro/s?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Sim, eliminar!",
                cancelButtonText: "Não, cancelar!",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function(isConfirm) {
                if (isConfirm) {
                    swal("Eliminado!", "O/s ficheiro/s foi/foram eliminado/s!", "success");
                } else {
                    swal("Cancelado", "A operação foi cancelada, os ficheiros foram salvos!", "error");
                }
            });
            break;
    }
}

I have tested only with Alert and confirm on onsubmit and everything went well. But when putting this script on onsubmit it does not "hold" the function, immediately deletes the data before confirming whether or not to delete the file.

2 answers

1

You can’t use it onsubmit="sweetalert(1)". The only way to expect a boolean return on onsubmit is using the confirm native (ugly and confused), which is synchronous (meaning that it "traps" the flow of code, as you said).

You need to place an intermediate function call there, cancel Ubmit, and wait for the interaction to manually submit, if applicable. Something like that:

<form id="meuForm" action="" method="post" onsubmit="sweetalert(1); return false;">

And there in his function(isConfirm){ you take the form and submit if you pass your verification:

document.getElementById('meuForm').submit();

Note: that switch in its function is totally unnecessary, can eliminate.

  • It didn’t work. : ss Can you do a Jsfiddle please?

  • @thecreator http://jsfiddle.net/7whfdcs5/. But there is no time to show the warning that the files have been deleted, the form is submitted first. If you want to prevent the page from reloading, you need to use ajax instead of submitting the form (which would be the subject of a separate question if you have questions about how to do it).

  • Still do not give, in your example everything works as I intend, but here not updated the code

  • I got it, there was a mistake in my code onsubmit="sweetalert(1);" return false;" -> onsubmit="sweetalert(1); return false"

1


Basic example.

HTML:

 <a href="delete.php?id=45454"  onClick="return confirm_delete('John');">
 <img src="../../../images/icones/effacer.png" border="0" alt="Deletar">
 </a>

JAVASCRIPT:

 function confirm_delete(msg)
 {
     var msg_total;
     if (msg == '')
     {
        msg_total = msg_glb_delete_start + "\n" + msg_glb_delete_end;
     }
     else
     {
        msg_total = msg_glb_delete_start + "\n( " + msg + " )\n"+ msg_glb_delete_end;
     }

     ret = confirm(msg_total);
     return ret;

 }

 msg_glb_delete_start = "Tem certeza que deseja apagar este registro?";
 msg_glb_delete_end = "Esta operação é definitiva. Clique em OK para continuar.";

Otherwise, if you want to use sweetalert obligatorily, you lack things in your code: you have:

 <form action="" method="post" onsubmit="sweetalert(1)">

when the code must be:

  <form action="" method="post" onsubmit="sweetalert(); return false;">

Missing ; and missing "Return false". Probably this is the problem.

  • The aim was to do this: http://jsfiddle.net/7whfdcs5/ with that Alert .. it is possible?

  • I’ve edited the answer, hoping it will help you.

  • It still doesn’t work, as soon as I give Submit, it eliminates/the line/as from the database..

  • Did I put the false Return? Another point, in your browser, uses the "Web Developer" ferment (if you use Firefox) to open the "console" and see Javascript error. Because in a lot of cases, when JS encounters an error, it still doesn’t take care of the rest of the code. Then you may have an error in another part of your code which makes your function is not executed.

  • Siim, pus.. The only error it gives is this: http://prntscr.com/5zb1tl

  • It wouldn’t be a problem with the library version you’re using?

  • I think the problem is right around the time I’m doing Submit, I UPDATED THE CODE TO UNDERSTAND

  • I got it, there was a mistake in my code onsubmit="sweetalert(1);" return false;" -> onsubmit="sweetalert(1); return false"

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.