How can I add confirmation dialog to a submit button in html5 form?

28,502

Have onsubmit attribute in your form tag like this if you just want a confirmation from user.

https://jsfiddle.net/yetn60ja/

<form id="id"
    method="POST" action="/y/b/"
    enctype="multipart/form-data"
    onsubmit="return confirm('Do you really want to submit the form?');"
>
    <input class="btn btn-primary"
        type="submit" name="submit" value="A"
    />
</form>

EDIT: Or try below code

<form id="id"
    method="POST" action="/y/b/"
    enctype="multipart/form-data"
>
    <input class="btn btn-primary"
        type="submit" name="submit" value="A"
        onclick="return confirm('Do you really want to submit the form?');"
    />
</form>
Share:
28,502
jartymcfly
Author by

jartymcfly

Updated on May 01, 2020

Comments

  • jartymcfly
    jartymcfly about 4 years

    I am creating an application in django and I have the next problem: I have a form in html with a submit button, but I want to show a confirmation dialog to select Yes or No before processing the information. How could I do it?

    This is my form code:

    <form id="id" method="post" action="/y/b/" enctype="multipart/form-data">
    
        {% csrf_token %} 
    
        {{ form.as_p }}
    
        <input class="btn btn-primary" type="submit" name="submit" value="A" />
    </form>
    

    Thank you so much!