Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirm before a form submit

Tags:

I have searched for an answer but couldn't find one!

I have a simple form,

<form action="adminprocess.php" method="POST">
    <input type="submit" name="completeYes" value="Complete Transaction" />
</form>

How would I adjust this to confirm before processing the form?

I tried onclick, but couldn't get it working.

Any ideas?

UPDATE - What I now have.

<script type="text/javascript">
var el = document.getElementById('myCoolForm');

el.addEventListener('submit', function(){
return confirm('Are you sure you want to submit this form?');
}, false);
</script>

<form action="adminprocess.php" method="POST" id="myCoolForm">
     <input type="submit" name="completeYes" value="Complete Transaction" />
</form>
like image 219
sark9012 Avatar asked Jun 27 '11 12:06

sark9012


People also ask

How do I confirm a form submit?

To confirm before a form submit with JavaScript, we can call the confirm function. to add a form. to select a form with querySelector . And then we set form.

How to give confirm alert in JavaScript?

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 .

How to use js confirm?

The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed. message is the optional string to be displayed in the dialog.

How to display confirm dialog box in JavaScript?

A confirmation dialog box is mostly used to take user's consent on any option. It displays a dialog box with two buttons: OK and Cancel. If the user clicks on the OK button, the window method confirm() will return true. If the user clicks on the Cancel button, then confirm() returns false.


2 Answers

HTML:

<form action="adminprocess.php" method="POST" id="myCoolForm">
    <input type="submit" name="completeYes" value="Complete Transaction" />
</form>

JavaScript:

var el = document.getElementById('myCoolForm');

el.addEventListener('submit', function(){
    return confirm('Are you sure you want to submit this form?');
}, false);

Edit: you can always use inline JS code like this:

<form action="adminprocess.php" method="POST" onsubmit="return confirm('Are you sure you want to submit this form?');">
    <input type="submit" name="completeYes" value="Complete Transaction" />
</form>
like image 180
Michael J.V. Avatar answered Sep 19 '22 21:09

Michael J.V.


<input type="submit" onclick="return confirm('Are you sure you want to do that?');">
like image 20
Greenisha Avatar answered Sep 18 '22 21:09

Greenisha