Call javascript onsubmit does not "trap" the submit until giving confirmation of eiliminação

1

I find the following error:

<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 already tested with the alert and confirm in the onsubmit and everything went well. But when I put this script in onsubmit it does not "lock" the function, it deletes the data before confirming whether or not I want to delete the file.

    
asked by anonymous 30.01.2015 / 21:47

2 answers

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, you will need things in your code: You have:

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

When the code should be:

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

Missing; and the value "return false" is missing. That's probably the problem.

    
31.01.2015 / 13:42
0

You can not use onsubmit="sweetalert(1)" . The only way to expect a Boolean return in onsubmit is by using the native% (undo and confusing)%, which is synchronous (which means that it "holds" the code stream, as you said).

You need to put an intermediary function call over there, cancel the submit, and wait for the interaction to, if necessary, submit manually. Something like this:

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

And there in your confirm you get the form and submit your verification:

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

Note: that function(isConfirm){ in your function is totally unnecessary, you can delete it.

    
30.01.2015 / 22:00