0

I need to confirm submit button, with cancel or ok.

If ok, it submits the form and performs the predefined operations.

If you cancel, close the message and stay on the same page.

My java script code is only running the submit and the operation is not canceled.

Thanks for the help.

function envio() {
  var r = confirm("Tem certeza que deseja excluir este registro?");
  if (r == true) {
    window.location = "edicao-demandas-result.lbsp";
    form.submit();
  }
}
<form method="post" name="form" action="file.html">
  <div class="w3-row-padding">
    <div class="w3-col w3-center w3-rest">
      <label class="w3-text-blue w3-small">ATTENTION...
      </label><br>
      <input type="submit" class="w3-comp-btn w3-blue w3-round"
          value="Delete Record" name=LBWEB_DELETERECORD onClick="envio();">
    </div>
  </div>
</form>
robinCTS
  • 5,746
  • 14
  • 30
  • 37
Jan Ferrer
  • 21
  • 7
  • i think you should use this method [https://stackoverflow.com/questions/6515502/javascript-form-submit-confirm-or-cancel-submission-dialog-box](https://stackoverflow.com/questions/6515502/javascript-form-submit-confirm-or-cancel-submission-dialog-box) – Jeisson Huerfano Jul 30 '17 at 01:00
  • window.location and submit..... you need to do one.... – epascarello Jul 30 '17 at 01:03

4 Answers4

1

You need to pass the event to your function and prevent the submit using preventDefault().

function envio(event) {
  event.preventDefault();
  var r=confirm("Tem certeza que deseja excluir este registro?");
  if (r==true) {
    window.location="edicao-demandas-result.lbsp";
    form.submit();
  }
}
<form method="post" name="form" action="file.html">
  <div class="w3-row-padding">
    <div class="w3-col w3-center w3-rest">
      <label class="w3-text-blue w3-small">
        ATTENTION...
      </label>
      <br>
      <input id="mySubmit" type="submit" class="w3-comp-btn w3-blue w3-round"
          value="Delete Record" name="LBWEB_DELETERECORD" onClick="envio(event);">
    </div>
  </div>
</form>
robinCTS
  • 5,746
  • 14
  • 30
  • 37
Jesus Carrasco
  • 1,355
  • 1
  • 11
  • 15
0

Because even if you catch the click event, you don't stop the form action. To do that, you need in your function to call event.preventDefault()

sheplu
  • 2,937
  • 3
  • 24
  • 21
0

You need a simple confirm with if statement:

if (confirm('text')) {
    alert('do stuff')
}

If you will click ok, the code in if block will be executed, if cancel - nothing else will happen, you just will stay at this page.

P.S.
  • 15,970
  • 14
  • 62
  • 86
0

This is happening because you are using onclick with a submit input. The default behavior of this type of element is always submit the form.

If you change the type to button it should work. Example:

<input type="button" class="w3-comp-btn w3-blue w3-round" value="Delete Record" name="LBWEB_DELETERECORD" onclick="envio();" />

This is how your code will look with this approach:

function envio() {
  var r=confirm("Tem certeza que deseja excluir este registro?");
  if (r==true) {
    form.submit();
  }
}
<form method="post" name="form" action="file.html">
  <div class="w3-row-padding">
    <div class="w3-col w3-center w3-rest">
      <label class="w3-text-blue w3-small">ATTENTION...</label>
      <br/>
      <input type="button" class="w3-comp-btn w3-blue w3-round" value="Delete Record" name="LBWEB_DELETERECORD" onclick="envio();" />
    </div>
  </div>
</form>
Gabriel Gomes
  • 348
  • 1
  • 8