[Django]-HTML – How to do a Confirmation popup to a Submit button and then send the request?

40πŸ‘

βœ…

I believe you want to use confirm()

<script type="text/javascript">
    function clicked() {
       if (confirm('Do you want to submit?')) {
           yourformelement.submit();
       } else {
           return false;
       }
    }

</script>
πŸ‘€DaneoShiga

217πŸ‘

The most compact version:

<input type="submit" onclick="return confirm('Are you sure?')" />

The key thing to note is the return

–

Because there are many ways to skin a cat, here is another alternate method:

HTML:

<input type="submit" onclick="clicked(event)" />

Javascript:

<script>
function clicked(e)
{
    if(!confirm('Are you sure?')) {
        e.preventDefault();
    }
}
</script>
πŸ‘€Isaac

7πŸ‘

Use window.confirm() instead of window.alert().

HTML:

<input type="submit" onclick="return clicked();" value="Button" />

JavaScript:

function clicked() {
    return confirm('clicked');
}
πŸ‘€Matt Ball

4πŸ‘

For a Django form, you can add the confirmation dialog inside the form tag:

<form action="{% url 'delete' %}" method="POST" onsubmit="return confirm ('Are you sure?')">
πŸ‘€user3693317

3πŸ‘

<script type='text/javascript'>

function foo() {


var user_choice = window.confirm('Would you like to continue?');


if(user_choice==true) {


window.location='your url';  // you can also use element.submit() if your input type='submit' 


} else {


return false;


}
}

</script>

<input type="button" onClick="foo()" value="save">
πŸ‘€Manjunath

2πŸ‘

Another option that you can use is:

onclick="if(confirm('Do you have sure ?')){}else{return false;};"

using this function on submit button you will get what you expect.

πŸ‘€Tiego Araujo

0πŸ‘

You can use onclick in the submit button:

<button type="submit" onclick="return confirm('Are you sure?');"/>Delete</button>

You can also use onsubmit in the form:

<form action="/" method="POST" onsubmit="return confirm('Are you sure?');">
  <button type="submit"/>Delete</button>
</form>
πŸ‘€doncadavona

0πŸ‘

Add the onClick function to the button

 <input type="submit" onclick="return confirm('Are you sure?')" />
πŸ‘€Alanso Mathew

Leave a comment