Confirm before sending form with Submit input

Asked

Viewed 1,617 times

2

I’m trying to make a button that only sends the form when I click on OK, but this one of mine, even though I click on Cancel, is being sent.

Javascript:

<script language="JavaScript"> 
function pergunta(){ 
   if (confirm('Tem certeza que quer enviar este pedido?')){ 
      document.enviarform.submit() 
   } 
} 
</script>

Knob:

<form name='apagar' id='apagar' action='formulario.php' method='post'>
<input type=submit value=enviar name=enviarform onclick='pergunta();'>

Where is the error?

Obs: when I put type=button in place of type=submit does no action (or sends, or anything).

Obs2: when in place of <input> place a <button>, send the same way (without giving the OK).

Anyone can help?

  • I put... keep sending even when I click cancel...

1 answer

3


Change the onclick putting return before the function. With this you will expect a response true or false function. If return true will send the form, if return false, the Submit is canceled:

function pergunta(){ 
   // retorna true se confirmado, ou false se cancelado
   return confirm('Tem certeza que quer enviar este pedido?');
}
<form name='apagar' id='apagar' action='formulario.php' method='post'>
   <input type=submit value=enviar name=enviarform onclick='return pergunta();'>
</form>

You don’t need document.enviarform.submit() because the action of the button is already from Ubmit.

  • It worked! Thank you!!!

  • Nothing young! See how to thank here -> [tour] ;)

Browser other questions tagged

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