Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am learning web development using Django and have some problems in where to put the code taking chage of whether to submit the request in the HTML code.

Eg. There is webpage containing a form(a blog) to be filled by the user, and upon click on the Save button,there is a pop up asking whether you want to confirm or not. If click on confirm, then the request is sent.

I searched and find this javascript code.

<script type="text/javascript"> function clicked() {     alert('clicked'); } 

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

But I guess this is not the correct function as it seems to me that whenever you click on the Button, the request will be submitted. So How can I delay the submit request until user has confirm the submit?

like image 332
Mona Avatar asked May 31 '13 03:05

Mona


People also ask

How do I add a confirmation pop up in HTML?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .

Can a HTML button perform a GET request?

You can: Either, use an <input type="submit" ..> , instead of that button. or, Use a bit of javascript, to get a hold of form object (using name or id), and call submit(..) on it. Eg: form.

How can I create a HTML button that submits and goes to the next page?

Just write/Declare your HTML Button inside HTML Anchor tags <a>. Anchor tags will make our HTML Buttons Clickable and after that, you can use Anchor tag's href attribute to give the Path to your Button.


1 Answers

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> 
like image 189
Isaac Avatar answered Oct 16 '22 18:10

Isaac