0

What is the best way to have an HTML form with two submit buttons and have them both do two different functions without using inline JavaScript? I can use Jquery so doing $('#id').submit(function (event) {}); would be fine. Thanks.

owwwerr
  • 13
  • 3
  • what is the issue? it seems like you already answered yourself. just use jquery for it and have different identifiers for each submit button – Ben Yitzhaki Jun 22 '17 at 17:02
  • you don't even need forms. You can use click functions if you are planning to use ajax – Grasper Jun 22 '17 at 17:03

2 Answers2

0

Don't make them <input type="submit">, make them <input type="button">.

If you are doing a basic HTTP POST, then add a click event that will

  1. change the action on the form
  2. call submit() on the form.

If you're submitting via Ajax, the click events will just manage the $.post() process for each button.

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
0

If you are posting the form data to the next page you could detect which button is clicked on, and assign a value to each button such as "button 1" value=1 and "button 2" value= 2. Then execute different functions on the page based on which button is clicked. Here is a similar question:

How do I use two submit buttons, and differentiate between which one was used to submit the form?

Will
  • 122
  • 1
  • 9