Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onsubmit() to show a confirmation if there are multiple submit buttons on the same form?

<form onsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')">
<input type='submit' name='delete' value='Undo' />
<input type='submit' name='no' value='No' />

when the user clicks on second submit button i.e No i want to display the confirmation dialogue as "Are you sure you want to commit the transaction."

like image 359
RatDon Avatar asked Aug 03 '13 22:08

RatDon


People also ask

How do you handle multiple submit buttons in the same form?

Create another button with type submit. Also add a 'formaction' attribute to this button and give it the value of the secondary URL where you want to send the form-data when this button is clicked. The formaction attribute will override the action attribute of the form and send the data to your desired location.

Can we have 2 submit buttons in a form?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How do I handle multiple submit buttons in a single form with laravel?

One of the best way is using an input with hidden type , then on clicking button append value to that input and get that value in request params in controller side . And then using if and else condition run your query.


2 Answers

<form method='post'>
    <input type='submit' name='undo' value='Undo' onclick="return confirm('Are you sure you want to rollback deletion of candidate table?')"/>
    <input type='submit' name='no' value='No' onclick="return confirm('Are you sure you want to commit delete and go back?')"/>
</form>

Worked fine. just changed onsubmit() to onclick(). as the function of both in this situation is same.

like image 125
RatDon Avatar answered Sep 22 '22 10:09

RatDon


You could bind to onclick instead of onsubmit - see below.

<script> 
function submitForm() {
    return confirm('Rollback deletion of candidate table?');
}
<script>

<form>
    <input type='submit' onclick='submitForm()' name='delete' value='Undo' />
    <input type='submit' onclick='submitForm()' name='no' value='No' />
</form>

Or alternately, using jQuery:

<script> 
$(document).ready(function() {
    $('form input[type=submit]').click(function() {
        return confirm('Rollback deletion of candidate table?');
    });
});
<script>
like image 27
Madison May Avatar answered Sep 22 '22 10:09

Madison May