0

I have the following form that redirects to a confirmation page, I would like to keep this behaviour when javascript is disabled in the browser however when javascript is enabled I would like to use a popup for confirmation. How can I disable the form action when javascript is enabled to just show the confirmation popup instead? ps: I must use the jquery ui-lightness plugin, that's why I'm not using bootstrap modals.

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="lib/jquery-ui-1.12.1.ui-lightness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
    <link href="lib/jquery-ui-1.12.1.ui-lightness/jquery-ui.theme.min.css" rel="stylesheet" type="text/css" />
    <link href="lib/jquery-ui-1.12.1.ui-lightness/jquery-ui.structure.min.css" rel="stylesheet" type="text/css" />
    <script src="lib/jquery-3.6.0.min.js" type="text/javascript"></script>
    <script src="lib/jquery-ui-1.12.1.ui-lightness/jquery-ui.min.js" type="text/javascript"></script>
</head>
<body>
    <script>
        $("#btn_trash").click(function() {
            var ret = null;
            $('#confirmDialog').dialog({
                resizable: false,
                height: 300,
                width: 500,
                modal: true,
                autoOpen: true,
                buttons: {
                    Oui: function() {
                        ret = "yes";
                        $(this).dialog("close");
                    },
                    Non: function() {
                        ret = "no";
                        $(this).dialog("close");
                    }
                },
                close: function() {
                    if (ret !== null)
                        $("#content").html("You answered <b>" + ret + "</b>.");
                    else
                        $("#content").html("You didn't answer.");
                }
            });
        });
    </script>


    <form id="delete_board_form" action='board/submit_delete' method='post'>
        <input type='text' name='delete' value='<?= $board->id ?>' hidden>
        <button id="btn_trash" class="btn" type="Submit">
          <svg class="bi text-white" width="20" height="20">
            <use xlink:href="assets/ressources/bootstrap-icons/bootstrap-icons.svg#trash" />
          </svg>
        </button>
    </form>
    <div id="content"></div>
    <div id="confirmDialog" title="Attention" hidden>
        <p>Confirmez-vous le lancement de cette opération irréversible ?</p>
    </div>

</body>
Tanuki
  • 183
  • 4
  • 11
  • 2
    Your question boils down to “how do I prevent a form submission with JavaScript”, and that is easily researchable. – CBroe Apr 12 '21 at 09:07
  • @CBroe Thank you for your help, I wasn't searching in those terms. You are right after researching in your terms the answer was quickly found! – Tanuki Apr 12 '21 at 09:27

1 Answers1

0

what worked for me was to use event.preventDefault()

<form id="delete_board_form" action='board/submit_delete' method='post' onsubmit="event.preventDefault()">
        <input type='text' name='delete' value='<?= $board->id ?>' hidden>
        <button id="btn_trash" class="btn" type="Submit">
          <svg class="bi text-white" width="20" height="20">
            <use xlink:href="assets/ressources/bootstrap-icons/bootstrap-icons.svg#trash" />
          </svg>
        </button>
    </form>
    <div id="content"></div>
    <div id="confirmDialog" title="Attention" hidden>
        <p>Confirmez-vous le lancement de cette opération irréversible ?</p>
    </div>
    ````
Tanuki
  • 183
  • 4
  • 11