I´m trying to include a confirmation dialog in my code, but I get stucked while implementing it. I want to add a button to my code that by clicking on it I get a confirmation dialog. Pressing the "yes-option" it should delete data in my database, while pressing the "no-option" should close the dialog box doing nothing else. But actually, when I run my code I get my dialog box and the confirmation yes/no options, but running the "no option" the submit code in html gets trigger too, so that by choosing "yes" or "no" I get the same effect and it delete always the data from my database.
This is my html code:
<form method="POST" action="/delete/prop" >
<p class="card-text h6"> <b>Typology: </b> <%= book.typology %> </p>
<input type="hidden" name="id5" value=<%= book.id %> />
<input type="hidden" name="prop" value= "typology" />
<input type="submit" value="Yes" class="button btn btn-danger" onclick="confirmAction()" />
</form>
And my javascript related to the dialog code:
<script type="text/javascript">
function confirmAction() {
let confirmAction = confirm("Are you sure to execute this action?");
if (confirmAction == true) {
alert("Action successfully executed");
} else {
alert("Action cancelled");
}
}
I found the solution in another website posted by @anavega.
She mentioned that I missed the return element in the html.
Here an example to test it :
function confirmAction() {
let confirmAction = confirm("Are you sure to execute this action?");
if (confirmAction == true) {
alert("Action successfully executed");
} else {
alert("Action cancelled");
}
}
<input type="submit" value="Yes" class="button btn btn-danger" onclick="return confirmAction()" />
Thank you for your help!