A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute using jQuery
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/










[...] More here: A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute controlling j… [...]
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!!
Hey, your article gives me a big help. Why using $(’form#myForm’) rather than $(’#myform’) though?? thank you very much
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.
Thank you for your reply!~many thanks mate
Brilliant, cheers mate. Really helped me out of a whole
that should have said a whole world of frustration!!
(pressed enter a little early)
this served me well, thanks!
Wow….great stuff… really helped a lot.. thanx buddy.
God save the internet and people like you!
Thank you very much!!