Different form of ACTION depending on the button pressed

I have a form, and I would like the ACTION field to differ depending on the button clicked.

For example, a form can be processed by various PHP files if I press button A or button B.

How can i do this?

Thanks!

+6
javascript html forms
source share
3 answers

In your buttons, you can simply set the form action using the form property , for example, on the a button:

 this.form.action = "fileA.php"; 

On the other hand:

 this.form.action = "fileB.php"; 

You can install this outside, for example:

 document.getElementById("buttonA").onclick = function() { document.getElementById("myForm").action = "fileA.php"; }; 

Or if you are using jQuery library:

 $(function() { $("#buttonA").click(function() { $(this).closest("form").attr('action', 'fileA.php'); }); }); 
+11
source share

Leave the action field blank:

 <form action ="" method="post" name="form1"> <input type ="submit" onclick="calA();"/> <input type = "submit" onclick="calB"/> </form> <script> function calA() { document.form1.action ="a.php"; } function calB() { document.form1.action = "b.php"; } </script> 
+2
source share

If you do not want to use Javascript but use HTML5, you can use the formaction attribute:

 <!DOCTYPE html> <html> <body> <form> <input type="submit" formaction="http://firsttarget.com" value="Submit to first" /> <input type="submit" formaction="http://secondtarget.com" value="Submit to second" /> </form> </body> </html> 
+1
source share

All Articles