HubSpot / vex

A modern dialog library which is highly configurable and easy to style. #hubspot-open-source
http://github.hubspot.com/vex/docs/welcome
MIT License
6.92k stars 494 forks source link

Submit Form ( POST ) after Button Clicked #229

Closed marcoczen closed 6 years ago

marcoczen commented 7 years ago

Hi,

I have a bootstrap form with a submit button as below;

``

``

When the button is clicked - the form is submitted. I would like a confirmation box (Yes - Delete, No ). I dont understand how to incorporate vex confirm into the button above from the documentation provided.

Any ideas ? Alert - Jquery newbie here.

timurjavid commented 6 years ago

You can prevent the submit event from happening in javascript by passing an event into an on click function. From the function, you can then handle your logic if you want the form to be submitted. The value of the vex dialog prompt is either true or false.

For example:

<form action="success.php" method="get" id="form1">
  Input1: <input type="text" name="input1"><br>
  Input2: <input type="text" name="input2"><br>
</form>

<button class="submit" type="submit" onclick="buttonFunction(event)" value="Submit">Submit</button>

<script>
         function buttonFunction(e) {
            e.preventDefault();
            vex.dialog.confirm({
                message: 'Do you want to delete this user?',
                className: 'vex-theme-plain',
                callback: function(value) {
                    if(value)
                    {
                        document.getElementById("form1").submit();
                    }
                }
            })
       }
</script>
bbatliner commented 6 years ago

Just because this is an old issue, I'm going to close it because @Timur2012's solution works (prevent the form submit in JS, confirm with vex and submit programmatically). Thanks!