A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute using jQuery

Date: 15th February 2009 at 4:48 pm | Filed under: development, scripts | Author: Sam Burdge | Tags: , , , , , ,

I was recently confronted with the problem of having a single html form with three different submit buttons, each button submitting the form data to a different php script. This is obviously impossible with html alone, as a form can only have one 'action' attribute, which dictates the URL of the file that the form's data is submitted to.

I searched around on the web for an easy way to implement this and found various examples. One of the best examples I found was on CoderLab's blog: Multiple submit buttons on a multiple blog which uses javascript to apply a different action attribute to the form depending on which button is pressed. Each button in the form has it's own onClick function like so:

<form name="myForm" id="myForm">
Search: <input type="text" id="wrdSearch"/>
<input type="button" name="google" id="google" value="Google" onClick="SendTo(this.id)"/>
<input type="button" name="msn" id="msn" value="MSN" onClick="SendTo(this.id)"/>
<input type="button" name="yahoo" id="yahoo" value="Yahoo" onClick="SendTo(this.id)"/>
</form>

(Visit the page to see the javascript that goes with it: http://blog.coderlab.us/2005/10/04/multiple-submit-buttons-in-a-form/)

As I already had jQuery installed on the site, and the CoderLab script would still need some modification to serve my specific purpose, I decided to see if I could find a simpler way to achieve a similar result using jQuery. The form on my site was a lot more complex, with many fields etc. So what I really needed was a script that would submit all the form data to each of the 3 php scripts depending on which submit button was clicked.

Firstly, I stripped the onClick functions out of the form and removed the name attributes from the buttons too, like so:

<form id="myForm" method="post">
Search: <input type="text" name="search"/>
<input type="button" id="button1" value="Submit to script 1" />
<input type="button" id="button2" value="Submit to script 2" />
<input type="button" id="button3" value="Submit to script 3" />
</form>

I then wrote the following jQuery script to change the form's action attribute accordingly and submit it when each button is pressed:

$(document).ready(function(){

$("#button1").click(function(){
$('form#myForm').attr({action: "script_1.php"});
$('form#myForm').submit();
});

$("#button2").click(function(){
$('form#myForm').attr({action: "script_2.php"});
$('form#myForm').submit();
});

$("#button3").click(function(){
$('form#myForm').attr({action: "script_3.php"});
$('form#myForm').submit();
});

});

The script should work on any browser that is compatible with jQuery (see here: http://docs.jquery.com/Browser_Compatibility)

For more information on installing and using jQuery visit the website: http://jquery.com/

10 Responses to “A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute using jQuery”

  • Comment by A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute using jQuery - PHP-update.co.uk
    Date: March 11th, 2009 at 1:55 pm

    [...] More here: A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute controlling j… [...]

  • Comment by Dickram
    Date: March 16th, 2009 at 6:33 pm

    You rock man. I was just looking for this and found your site. I’m using jquery too, with code igniter.
    Thank you very much!!

  • Comment by David
    Date: September 24th, 2010 at 1:04 am

    Hey, your article gives me a big help. Why using $(’form#myForm’) rather than $(’#myform’) though?? thank you very much

  • Comment by Sam Burdge
    Date: September 24th, 2010 at 1:17 am

    Hi David
    Glad it helped ;)

    there is no difference between using $(’form#myForm’) or $(’#myform’)

    i just wrote it that way as i thought it would make it clearer for the tutorial.

  • Comment by David
    Date: September 24th, 2010 at 4:25 am

    Thank you for your reply!~many thanks mate

  • Comment by Ben
    Date: October 4th, 2010 at 10:12 am

    Brilliant, cheers mate. Really helped me out of a whole

  • Comment by Ben
    Date: October 4th, 2010 at 10:13 am

    that should have said a whole world of frustration!!

    (pressed enter a little early)

  • Comment by Tom Lackey
    Date: April 27th, 2011 at 9:21 pm

    this served me well, thanks!

  • Comment by Vishal
    Date: June 10th, 2011 at 9:50 am

    Wow….great stuff… really helped a lot.. thanx buddy.

  • Comment by Mr. Serra
    Date: June 17th, 2011 at 2:57 pm

    God save the internet and people like you!
    Thank you very much!!

Leave a Comment